将设为首页浏览此站
开启辅助访问 天气与日历 收藏本站联系我们切换到窄版

易陆发现论坛

 找回密码
 开始注册
查看: 381|回复: 2
收起左侧

openstack使用ansible nova配置

[复制链接]
发表于 2021-9-3 15:45:36 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?开始注册

x
/usr/lib/python2.7/site-packages/openstack/compute/v28 H; v" M" k1 B' N3 O* P
server.py
; l: l$ D/ y2 Y3 L2 ?' c9 C文件内容:
8 B' q" o% ?' X& T! U% p9 n7 ~! n6 U" i; G7 R
# Licensed under the Apache License, Version 2.0 (the "License"); you may5 l: }: i2 L. C$ _
# not use this file except in compliance with the License. You may obtain
3 d: A# m; ^+ O3 U4 n4 x# a copy of the License at
" s/ l, y2 v& U. G* K% ]#
5 A! E' ~7 ~/ ~, P5 b- D) A#      http://www.apache.org/licenses/LICENSE-2.0! D, T+ Z6 g! N/ {* `# t
#
! x" h1 y2 w: o! z# Unless required by applicable law or agreed to in writing, software# R# ^" j/ s4 k5 ~
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
5 V& l2 G  o- ?) k' `* T! Z; A# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
( `8 P, g5 a* {7 s* |# License for the specific language governing permissions and limitations
# p' _+ g0 G) y# under the License.) T3 U, v/ I% m& t$ {) P! u
from openstack.compute.v2 import metadata
5 D3 q  K* ^. e1 P  _8 a& mfrom openstack.image.v2 import image$ y$ b5 V) D3 Z% L( H: t
from openstack import exceptions
0 r1 \$ @1 G0 j$ }; L+ Efrom openstack import resource
% A' S5 z# p( Lfrom openstack import utils  ^* m! u) ^5 Y% I! k' }: _1 ?/ g/ i
CONSOLE_TYPE_ACTION_MAPPING = {
6 _/ T3 W" c* v3 {: o. n! @    'novnc': 'os-getVNCConsole',9 y8 `( Y8 d% s9 }6 W9 |3 A' Y
    'xvpvnc': 'os-getVNCConsole',
5 n( n6 \$ H9 b. D& W5 Y3 d    'spice-html5': 'os-getSPICEConsole',
( q" t' g$ h3 i& K. |$ t! Y    'rdp-html5': 'os-getRDPConsole',
+ y" C* ^' \6 F5 K    'serial': 'os-getSerialConsole'- F  N& q6 D6 f; I
}- Y/ U% \5 E  A) S9 F4 k( s
class Server(resource.Resource, metadata.MetadataMixin, resource.TagMixin):0 I, g5 w) I: s9 T7 m& z7 \
    resource_key = 'server'; C5 t$ i, x+ u4 E0 V/ F
    resources_key = 'servers'* C8 m, \8 B3 M$ z  g! u
    base_path = '/servers'+ l! \6 O) _7 _9 D/ z, B9 x: l
    # capabilities
& z) W" [7 {1 l' Y3 L. J    allow_create = True) D' S$ [; S8 t8 ^
    allow_fetch = True8 ]9 P/ |* N9 q6 V
    allow_commit = True) H6 V6 U! h* ?7 i
    allow_delete = True; B5 o) S1 P/ d4 ^
    allow_list = True3 y. p  i$ X2 H4 w% E; h) ^6 W! L
    _query_mapping = resource.QueryParameters() [( G! H6 ~3 ?3 I  k
        "auto_disk_config", "availability_zone"," o: [( n8 U$ z$ c0 R
        "created_at", "description", "flavor",/ S/ I% i; J! b, B: Y3 p  P' u8 v
        "hostname", "image", "kernel_id", "key_name",
7 P8 ?: u) r7 Z; T) w        "launch_index", "launched_at", "locked_by", "name",( p2 @% K2 z7 {( t& g+ x
        "node", "power_state", "progress", "project_id", "ramdisk_id",5 C8 g" R1 T! G2 j& C2 s6 i$ l( w
        "reservation_id", "root_device_name",
+ ~8 n! Y& [0 G2 Q' _; p* d1 t        "status", "task_state", "terminated_at", "user_id",
9 A# q, U. d6 G* q1 B        "vm_state",# u$ [0 k/ Q9 ^0 e  i$ Y
        "sort_key", "sort_dir",
& _( A/ o% ?4 q( O' ]        access_ipv4="access_ip_v4",
# r: s1 D6 p# N! H- |& N, H        access_ipv6="access_ip_v6",
5 I/ y. f  O1 K        has_config_drive="config_drive",
7 Q" Q# e+ B6 z+ ]' a+ x        deleted_only="deleted"," K) y$ E! j' ~' ^
        compute_host="host",6 }5 |) j/ T& ^" b" h. P
        is_soft_deleted="soft_deleted",! O. A( [  |3 a9 ?" Y# {
        ipv4_address="ip",% t" q* e2 {) K! y1 \% I7 k
        ipv6_address="ip6",
8 x# `8 k9 A2 U% w  ^# H6 F        changes_since="changes-since",$ O6 Y( R) J6 ]8 b5 K
        changes_before="changes-before",! J3 u) ]6 D0 D
        id="uuid",
. C. L" i/ y  U$ O) r  N: T, C% e        all_projects="all_tenants",1 G, J8 B2 m! D
        **resource.TagMixin._tag_query_parameters# g: M/ {6 B0 m
    )
" R% A. J& S# C5 W8 C0 m    _max_microversion = '2.72', K; k4 S- C& ~- y
    #: A list of dictionaries holding links relevant to this server.6 D; E$ X1 e, _0 y& }' Y
    links = resource.Body('links')0 U0 L8 q; w+ |, r( d
    access_ipv4 = resource.Body('accessIPv4')6 z- O4 X8 Y; v9 L5 n) u
    access_ipv6 = resource.Body('accessIPv6')/ h  Z9 ^( R& B& S  z6 y
    #: A dictionary of addresses this server can be accessed through.1 {" m7 i* ~7 U1 ^. N) J6 ?( L
    #: The dictionary contains keys such as ``private`` and ``public``,
) F; G! }6 q! o; V    #: each containing a list of dictionaries for addresses of that type.8 F# y& ]- w+ V: e
    #: The addresses are contained in a dictionary with keys ``addr``! l3 M8 F1 T# @- d, e
    #: and ``version``, which is either 4 or 6 depending on the protocol) S; L. P1 i( Y- \7 X; [$ X
    #: of the IP address. *Type: dict*) H- k$ Z6 _- G$ c: m6 E
    addresses = resource.Body('addresses', type=dict)
- e: n- Y( f  M0 T: F9 u    #: When a server is first created, it provides the administrator password.
: m8 Q7 k! O& v% \+ ?    admin_password = resource.Body('adminPass')
) Z2 |; ]8 j; G$ B    #: A list of an attached volumes. Each item in the list contains at least
9 L) |3 m! ?/ c1 Q5 s    #: an "id" key to identify the specific volumes.
2 A3 B6 a7 Y1 S+ \    attached_volumes = resource.Body(
9 s0 [7 v/ L6 L3 j        'os-extended-volumes:volumes_attached')0 f" C6 x1 Y/ e. A% h
    #: The name of the availability zone this server is a part of.
* T/ g/ W  q8 j+ ?    availability_zone = resource.Body('OS-EXT-AZ:availability_zone')  v9 l. v  k- a
    #: Enables fine grained control of the block device mapping for an0 V+ n2 R1 N3 G9 E! S
    #: instance. This is typically used for booting servers from volumes.* C, I$ Q" x0 F: n0 K0 e9 t
    block_device_mapping = resource.Body('block_device_mapping_v2')
# K2 i' w) ~  c( `' w1 \. c- x    #: Indicates whether or not a config drive was used for this server.; J+ N2 X' C) ~. l
    config_drive = resource.Body('config_drive')
( ]) N/ d$ U: n' P: U    #: The name of the compute host on which this instance is running.8 z5 S4 B/ Z3 Y8 p8 _
    #: Appears in the response for administrative users only.
5 H& W) h5 o" n* @) h: _    compute_host = resource.Body('OS-EXT-SRV-ATTR:host')
/ a3 `0 Q, B* U) _    #: Timestamp of when the server was created.
3 K- @7 {3 w, N& p# c. }    created_at = resource.Body('created')
% o" ~4 H/ J( M. k2 s. r    #: The description of the server. Before microversion% W1 z& `5 W) p
    #: 2.19 this was set to the server name.
% u/ B$ `. W. B% H0 [    description = resource.Body('description')2 C; p2 W9 z' }
    #: The disk configuration. Either AUTO or MANUAL.
% T7 t, I* [2 z  k( ~* }    disk_config = resource.Body('OS-DCF:diskConfig')) A+ v* e3 I% e  K
    #: The flavor reference, as a ID or full URL, for the flavor to use for
) `% J( @+ Y: Q    #: this server.
* }: N- E) ^, \! [    flavor_id = resource.Body('flavorRef')
8 e7 ~( K" R) _    #: The flavor property as returned from server.( A3 W/ B- x. s2 u
    # TODO(gtema): replace with flavor.Flavor addressing flavor.original_name
' n3 [+ p) ^8 {1 r    flavor = resource.Body('flavor', type=dict)2 T+ W' V" Z7 p& C6 r
    #: Indicates whether a configuration drive enables metadata injection.8 ?1 R8 V( n! O. M, R
    #: Not all cloud providers enable this feature.
, ^4 G2 B( j6 j/ n    has_config_drive = resource.Body('config_drive'): F* p. u/ I& L5 B$ }/ l* U
    #: An ID representing the host of this server.% y- Z, W5 V) v# i; s7 h
    host_id = resource.Body('hostId')1 B+ V$ O5 q* N1 E
    #: The host status.
0 b* W1 j! _: W- {) j! a2 \3 V    host_status = resource.Body('host_status')
2 a* L5 e) H5 A7 O5 e) c# J    #: The hostname set on the instance when it is booted.
6 B1 s5 t9 Y& t. o7 `    #: By default, it appears in the response for administrative users only.; [5 O/ \8 O" K( P
    hostname = resource.Body('OS-EXT-SRV-ATTR:hostname')2 o3 f- c: b" Q- `) [9 B' o
    #: The hypervisor host name. Appears in the response for administrative" \3 W1 M( f& D! V, K
    #: users only.
* v9 ^# B& `7 G    hypervisor_hostname = resource.Body('OS-EXT-SRV-ATTR:hypervisor_hostname')& B( `9 L% x; }  }2 b$ l
    #: The image reference, as a ID or full URL, for the image to use for
7 m3 h3 D: f2 P/ v% U/ k5 X8 t    #: this server.# Q2 M4 i/ R& _/ z; \  m- j3 T
    image_id = resource.Body('imageRef')
  I# j* r/ R& n( l' g& B: u& I    #: The image property as returned from server.- i, @/ p$ \' `, \- [
    image = resource.Body('image', type=image.Image)
) |0 M% R0 B* @, |    #: The instance name. The Compute API generates the instance name from the; D* e7 R4 h6 x* G- D
    #: instance name template. Appears in the response for administrative users. V! j* q) y  }: ?
    #: only.
* w3 t0 O$ O/ z% Y- T    instance_name = resource.Body('OS-EXT-SRV-ATTR:instance_name')7 J7 c7 B& d* N3 S+ ?
    # The locked status of the server
2 b7 g0 E" n& c$ R  u# V: C    is_locked = resource.Body('locked', type=bool)  y1 S. e7 y) K
    #: The UUID of the kernel image when using an AMI. Will be null if not.
6 P: b( D8 x8 i" f& m* U    #: By default, it appears in the response for administrative users only.+ B& W- q/ O' L" l! e
    kernel_id = resource.Body('OS-EXT-SRV-ATTR:kernel_id')
' G. k- N5 F. o0 i4 m, f    #: The name of an associated keypair: o5 r+ }2 G0 A( W8 T% A
    key_name = resource.Body('key_name'). T4 P5 e: S: f9 V/ u" D- D$ M# Z. A
    #: When servers are launched via multiple create, this is the
4 S( s0 B4 ?* o5 y. b    #: sequence in which the servers were launched. By default, it% p# r% O5 e1 f. S/ ]7 L. G+ Z
    #: appears in the response for administrative users only./ i2 @9 V: {5 S6 J8 l: }
    launch_index = resource.Body('OS-EXT-SRV-ATTR:launch_index', type=int)
9 T1 w- ~+ X- r  f/ U9 V' C    #: The timestamp when the server was launched.
) u0 u0 W3 }9 o1 X- u    launched_at = resource.Body('OS-SRV-USG:launched_at')% z1 O& k9 F; D- w2 p
    #: Metadata stored for this server. *Type: dict*% x: P! U- k& u% ?: ]$ ^1 w
    metadata = resource.Body('metadata', type=dict)
4 @* Z% a( u* D; Q8 ?2 t5 Q    #: A networks object. Required parameter when there are multiple2 z4 A0 a2 N3 Y7 j8 c' M: ]. N
    #: networks defined for the tenant. When you do not specify the
$ |8 Z* T- r# _8 p) w8 M    #: networks parameter, the server attaches to the only network" V9 _. {9 o3 E5 l- A
    #: created for the current tenant.
1 Z( e0 d' i) y  s    networks = resource.Body('networks')& ^0 _5 E- Y' K% O& j
    #: The file path and contents, text only, to inject into the server at( }! s. p% R* N$ b
    #: launch. The maximum size of the file path data is 255 bytes.
$ u% _$ n% ~( o    #: The maximum limit is The number of allowed bytes in the decoded,
9 U8 B& t. I+ H0 k, j    #: rather than encoded, data.; y' {* A: F# G6 \+ s$ _9 i' M
    personality = resource.Body('personality')
- W- P% l6 u* y, [: I6 u$ f- p    #: The power state of this server.. m/ u, P. s* D8 _( b0 W
    power_state = resource.Body('OS-EXT-STS:power_state')8 k) {1 z+ X0 Y! y2 O3 `5 Y
    #: While the server is building, this value represents the percentage
% O; n& {; }. k% E    #: of completion. Once it is completed, it will be 100.  *Type: int*
9 a0 C4 Z; {1 T, ~- O    progress = resource.Body('progress', type=int)
! }" L5 u7 k7 Q9 J    #: The ID of the project this server is associated with.
. O& S! R+ p, {- j    project_id = resource.Body('tenant_id')
: \9 n/ V" ~2 l$ R7 E" k    #: The UUID of the ramdisk image when using an AMI. Will be null if not.
" V2 f1 K* {+ j" \! M! r    #: By default, it appears in the response for administrative users only.! c" V) _- x; l/ [( y
    ramdisk_id = resource.Body('OS-EXT-SRV-ATTR:ramdisk_id')
, E3 i6 A7 f, j: `7 K, ?$ ]    #: The reservation id for the server. This is an id that can be
+ w& F3 z, t$ f8 F: A    #: useful in tracking groups of servers created with multiple create,; X3 Q0 H) i2 w, T8 o* N& T, `
    #: that will all have the same reservation_id. By default, it appears+ I) A! [. a# B% F% m6 _) k/ L) _
    #: in the response for administrative users only.
  p2 v+ [2 e4 B    reservation_id = resource.Body('OS-EXT-SRV-ATTR:reservation_id')2 }0 U' K$ V, t3 y, i! Z# u; l
    #: The root device name for the instance By default, it appears in the
8 S! i0 k9 a6 v% Z0 ^8 k* f* W    #: response for administrative users only.
2 F( [' j7 a( y0 k0 f1 d9 K    root_device_name = resource.Body('OS-EXT-SRV-ATTR:root_device_name')
! K& K+ }/ X! k7 @2 g$ }9 I1 l: N    #: The dictionary of data to send to the scheduler.
# r0 D2 R0 c( o  O' p# q1 `6 {9 a    scheduler_hints = resource.Body('OS-SCH-HNT:scheduler_hints', type=dict)
, E% r: r/ ]0 D  f  Y    #: A list of applicable security groups. Each group contains keys for
% A) ?4 j; y7 `" S. h    #: description, name, id, and rules.2 P- A# i9 _/ [$ @
    security_groups = resource.Body('security_groups',
! M; v% h- P. }0 e+ ^5 Q                                    type=list, list_type=dict)
% v/ @) Q( k1 B) d$ R2 }/ u    #: The UUIDs of the server groups to which the server belongs.
0 G! j) w7 f+ T- q4 E& ]/ {    #: Currently this can contain at most one entry.
$ [: _0 {7 Y9 O. b    server_groups = resource.Body('server_groups', type=list, list_type=dict)/ m& L+ ?7 H- y- m7 H' r
    #: The state this server is in. Valid values include ``ACTIVE``,& S- L5 X9 V& f, [5 {4 d4 U) a
    #: ``BUILDING``, ``DELETED``, ``ERROR``, ``HARD_REBOOT``, ``PASSWORD``,
( G/ M' L" R/ C+ _% A    #: ``PAUSED``, ``REBOOT``, ``REBUILD``, ``RESCUED``, ``RESIZED``,1 {+ L; c, P) M; l0 q- k8 O0 R" H
    #: ``REVERT_RESIZE``, ``SHUTOFF``, ``SOFT_DELETED``, ``STOPPED``,
% a9 G' u+ }/ J% P& r  a    #: ``SUSPENDED``, ``UNKNOWN``, or ``VERIFY_RESIZE``.
: z3 n; a; a2 i/ l% X5 ?! S7 b" s' ^    status = resource.Body('status')! R+ P3 A6 F- t! a; v& k  l2 j
    #: The task state of this server.
; Q9 P5 \% _' K3 U& o    task_state = resource.Body('OS-EXT-STS:task_state')
1 b+ E4 F6 f' Y% f- R  ?, c    #: The timestamp when the server was terminated (if it has been).
9 j( }' q* x& S" V0 a/ M( G    terminated_at = resource.Body('OS-SRV-USG:terminated_at')
8 G9 N& ~- ?7 h* P% U$ v    #: A list of trusted certificate IDs, that were used during image# ?  [4 N9 q6 a; Z5 H8 y5 v. v
    #: signature verification to verify the signing certificate.* T3 U2 l# S8 j  Q0 G7 Z$ g$ o
    trusted_image_certificates = resource.Body(
* F+ E' S9 k1 M2 H- H' V7 ~3 J+ b& U3 O        'trusted_image_certificates', type=list)
4 A- ^& u& g0 s( {5 o    #: Timestamp of when this server was last updated.
& E9 q/ X7 L0 O5 e# S4 s! ?    updated_at = resource.Body('updated')% E5 `9 d3 m$ d6 i7 [" g" k, H
    #: Configuration information or scripts to use upon launch.
& m9 q) H/ I0 w' o# ]    #: Must be Base64 encoded.
3 [1 C! }. W  M# `$ l  w$ G    user_data = resource.Body('OS-EXT-SRV-ATTR:user_data')3 }; c& f: t6 V
    #: The ID of the owners of this server.& i2 g& Y) a# d, @8 u* {
    user_id = resource.Body('user_id')" p, m- E5 Z1 ~  ?) U
    #: The VM state of this server.! T8 N" O) P+ D" G; M+ J8 W$ ?
    vm_state = resource.Body('OS-EXT-STS:vm_state')! V% V( J2 E, n# g+ a+ w
    def _prepare_request(self, requires_id=True, prepend_key=True,4 v4 [$ C  ?6 Y6 K4 Q
                         base_path=None, **kwargs):
4 C! C+ V$ u+ X* Y4 ]        request = super(Server, self)._prepare_request(requires_id=requires_id,8 b6 S' A/ V! l1 s; e/ W4 O! ?
                                                       prepend_key=prepend_key,! C( @9 u; \7 x  B  ~4 N- w; x, a
                                                       base_path=base_path)
8 [% y" e/ a2 X6 B        server_body = request.body[self.resource_key]
* U+ o4 y- a& l3 G! }% |        # Some names exist without prefix on requests but with a prefix7 p' m- a0 \8 c& l8 [6 G: N! \
        # on responses. If we find that we've populated one of these, p1 a& ~( J( a9 _+ j9 F
        # attributes with something and then go to make a request, swap out( U- H) C8 d) c% ~
        # the name to the bare version.
4 S' P, [; M; N5 h/ Q        # Availability Zones exist with a prefix on response, but not request; y/ f2 Z1 D% x7 M' r8 e
        az_key = "OS-EXT-AZ:availability_zone"
+ m/ ]* b' |4 ]0 g2 s; n) J        if az_key in server_body:
8 n8 f1 |$ q5 z& Y2 D3 D$ _" h            server_body["availability_zone"] = server_body.pop(az_key)+ a2 e7 z6 b5 T$ i. `
        # User Data exists with a prefix on response, but not request7 Q* v( V, w; K3 d$ @/ j7 e5 N; |
        ud_key = "OS-EXT-SRV-ATTR:user_data"
! L, q( M1 G7 A        if ud_key in server_body:! M  ^: l( \% H7 u
            server_body["user_data"] = server_body.pop(ud_key)
  C6 K5 U0 @8 }, e0 H& m        # Scheduler hints are sent in a top-level scope, not within the
: f/ F- M& a; c4 R$ A5 b8 k        # resource_key scope like everything else. If we try to send  `) A  T0 n$ N: f6 [
        # scheduler_hints, pop them out of the resource_key scope and into  d* q& N3 o2 ?, }
        # their own top-level scope.
1 V% K& ^7 E* i. N0 d        hint_key = "OS-SCH-HNT:scheduler_hints"5 @; B( a" [+ H$ k6 U
        if hint_key in server_body:
! _/ X4 r: }& _5 l2 S            request.body[hint_key] = server_body.pop(hint_key)
( l! [8 [$ v* I- j7 v        return request- F8 C( z# T; e3 D
    def _action(self, session, body, microversion=None):4 n7 y& u2 G' r4 n+ q6 }
        """Preform server actions given the message body."""& F/ d7 s9 ~( Y9 P% O. M
        # NOTE: This is using Server.base_path instead of self.base_path
" V! R" Z; n. ~" p+ `        # as both Server and ServerDetail instances can be acted on, but! A! i3 }* n& r
        # the URL used is sans any additional /detail/ part.
; g$ s# w# h5 P+ `        url = utils.urljoin(Server.base_path, self.id, 'action')
8 P7 b1 I- J& ~$ s( c: \        headers = {'Accept': ''}
/ o: K: I+ @5 H6 }7 d        response = session.post() i1 i- G8 n3 L% y
            url, json=body, headers=headers, microversion=microversion). K* [9 l1 X8 S8 A" l( J
        exceptions.raise_from_response(response)" x" y* E" x9 o' }1 O
        return response: \0 V. a2 m4 o# j$ H) j
    def change_password(self, session, new_password):, L* \: g7 C, {# O  y( @
        """Change the administrator password to the given password."""1 W- V* T7 g2 U$ I# g
        body = {'changePassword': {'adminPass': new_password}}& N- D! U- H7 b7 K
        self._action(session, body)) w0 }& v. z$ A' A4 K8 j# C. y
    def get_password(self, session):) o, \, S/ n# H* e7 }" y" X* n  @
        """Get the encrypted administrator password."""7 Q1 ~; w9 k4 F7 F+ C6 m
        url = utils.urljoin(Server.base_path, self.id, 'os-server-password')
0 _: P6 x. I; X' a' Y+ F8 R7 |9 O        return session.get(url)# ?8 @' U9 g; I" I, ?$ B, i5 K
    def reboot(self, session, reboot_type):
! {/ t3 r4 o5 }+ i- I        """Reboot server where reboot_type might be 'SOFT' or 'HARD'.""", J9 r5 n; n9 J
        body = {'reboot': {'type': reboot_type}}
+ `  R. I, u9 \9 o9 B        self._action(session, body)
! _% F5 T1 }2 A: v# B; m3 f) |    def force_delete(self, session):6 v7 p, r! ~8 n
        """Force delete a server."""1 Z! w2 Y- w- ~" M7 s
        body = {'forceDelete': None}1 `, v( Y" R( }3 I+ d4 u& w: k
        self._action(session, body)2 r  U( j# k6 g/ m8 ]2 h
    def rebuild(self, session, name, admin_password,3 a; |6 {7 o6 [- d" U  w+ g
                preserve_ephemeral=False, image=None,
; m9 i8 H  b! P) s0 d5 y  F& L                access_ipv4=None, access_ipv6=None,
7 s4 u' h5 i! R* c5 a                metadata=None, personality=None):
1 X' h2 m, ^" o% g        """Rebuild the server with the given arguments."""
9 N# x  g# T: A        action = {
# V/ |% B% [& a; |  L& F            'name': name,
* ~5 `" |0 u: W0 I) w+ H            'adminPass': admin_password,
4 r0 I' E" q7 ?5 K# A( {            'preserve_ephemeral': preserve_ephemeral
/ M4 v4 Z! v2 H. r5 i3 W( I& c& z$ `: F        }
, S; K3 c2 i# F5 T1 V' {        if image is not None:
- l" R* @3 Z0 i7 t/ Y            action['imageRef'] = resource.Resource._get_id(image)
) T& o8 d, W' O0 ?/ a6 t+ m' h        if access_ipv4 is not None:. z0 _' \$ d' c& H8 D* ^  K5 R
            action['accessIPv4'] = access_ipv4
' i) a3 ~+ y# S5 M% D        if access_ipv6 is not None:
' J3 a- {6 w  ]& s            action['accessIPv6'] = access_ipv6( J% T/ A' `9 [- u1 I, U0 q7 M
        if metadata is not None:
# l; P6 r/ f  L$ Z3 J) R8 B7 D, M' n            action['metadata'] = metadata
1 ?- O2 ~- Z/ H7 o  J1 J        if personality is not None:1 d8 S5 Z; k5 W% m" g  E
            action['personality'] = personality0 l8 z' P, P1 r2 R+ c
        body = {'rebuild': action}
( _# o$ E2 G+ {        response = self._action(session, body)
$ O1 o4 b+ d9 s/ ]* Y! r1 J' y3 O        self._translate_response(response)
# V$ |7 d  g! H1 ?        return self
! W6 t5 o9 t- [4 y, \    def resize(self, session, flavor):
( L1 y. _6 O$ J, h. P9 _' y6 h9 k        """Resize server to flavor reference."""
6 [. }& b6 C( o6 I        body = {'resize': {'flavorRef': flavor}}, s$ [1 Z' H4 j
        self._action(session, body)9 h& E+ v  f( E
    def confirm_resize(self, session):) c' Q) {# g/ p+ U& w9 K
        """Confirm the resize of the server."""" D( K5 q$ g/ l- Y
        body = {'confirmResize': None}6 Q, F7 o+ n7 i* ~6 G/ [' N! M
        self._action(session, body)# Q  ~9 t3 |% E% O) n0 l* }; Y
    def revert_resize(self, session):+ {4 S7 ?7 I5 e- z6 p) I
        """Revert the resize of the server."""- t. r1 |# U) u  k  o& h: i) H
        body = {'revertResize': None}0 o5 q4 l; I7 o% O, D, h( c& ^
        self._action(session, body)2 N) T8 S# L4 J1 @9 t. y/ l7 `
    def create_image(self, session, name, metadata=None):
8 q6 O! P, y" J, ~        """Create image from server."""
5 H: t# _& v* m: p        action = {'name': name}% a: u+ b6 \4 l- P0 d( D% s
        if metadata is not None:. p$ n1 W2 L- j3 u) \
            action['metadata'] = metadata
4 W' ]: m4 ]) p* w* `        body = {'createImage': action}
+ j* E+ @3 p, \        # You won't believe it - wait, who am I kidding - of course you will!
* y. _/ m3 \; w& V: h  @3 }        # Nova returns the URL of the image created in the Location+ z" c9 t- i) |# J& }6 X( |, }+ ^( ?
        # header of the response. (what?) But, even better, the URL it responds+ Z- B4 h* w! G: B6 P
        # with has a very good chance of being wrong (it is built from' d3 ?2 u& p2 v, L0 T- }
        # nova.conf values that point to internal API servers in any cloud
$ z2 I4 W4 j. ?4 D$ @" q/ l* \9 D        # large enough to have both public and internal endpoints.: @/ v- e0 H: F3 P& _
        # However, nobody has ever noticed this because novaclient doesn't' }3 T4 j4 y9 d8 h7 C( X
        # actually use that URL - it extracts the id from the end of
: d4 j9 A# Z) d% \6 K% S        # the url, then returns the id. This leads us to question:
* q" P6 {5 a+ K9 n2 x        #   a) why Nova is going to return a value in a header
# f2 Y. r) U- f# m) N        #   b) why it's going to return data that probably broken
- Z3 ~9 N' Z, F4 b        #   c) indeed the very nature of the fabric of reality
8 w1 `. g# h& P8 |. `. _7 ^' g5 R        # Although it fills us with existential dread, we have no choice but2 j! P' F: O' p, L
        # to follow suit like a lemming being forced over a cliff by evil5 @7 ]5 [# i: j; c7 z
        # producers from Disney.. f2 G! O3 Q0 j/ P* J8 A! L4 W# x
        microversion = None
" O$ t2 R2 ]. K! c        if utils.supports_microversion(session, '2.45'):. }& F) ~4 |) E5 M6 g/ f' B
            microversion = '2.45'
* p: h6 o. Z) ^9 ]        response = self._action(session, body, microversion)
* F4 e  l1 P: }+ l5 w        body = None9 W6 a7 B: r  w# o+ H
        try:7 R2 A! x; ]' k' M) q$ n+ r8 y0 a& l
            # There might be body, might be not9 y  C- s/ p0 o! m" t
            body = response.json()5 l4 ^$ L; r) J9 ?: H9 Z9 I1 v$ s, e4 U& M1 ~
        except Exception:
6 C1 i3 S# \, s1 d" ~" q3 K            pass
" _. q4 i. A( |# G9 X$ u- K! q, c        if body and 'image_id' in body:1 w- f, u  ^7 z+ C9 b! @$ C
            image_id = body['image_id']+ R% x6 _) D& d' h3 L3 P9 i
        else:
+ E8 _; ?( E% ~( y9 }9 R) ?            image_id = response.headers['Location'].rsplit('/', 1)[1]
6 ~/ o: _6 A, v! C. ?* @6 A        return image_id$ }( h( ~' g9 q( C. T
    def add_security_group(self, session, security_group_name):
2 z6 f* U+ `2 ]9 v" [        body = {"addSecurityGroup": {"name": security_group_name}}
) |, r" [+ H+ R. q+ i        self._action(session, body)
& v- T5 ~$ H8 B. x' n    def remove_security_group(self, session, security_group_name):5 P# t! L& e- q# Q+ j: ?! i
        body = {"removeSecurityGroup": {"name": security_group_name}}8 ^! c. W( q% T  I1 h. M# P1 I1 z$ ^
        self._action(session, body)% O0 e0 r3 A* v6 u: E& o7 T( b
    def reset_state(self, session, state):
/ ?1 `. `; n% B3 w7 B        body = {"os-resetState": {"state": state}}
0 Q) ^0 I( R/ C& ]        self._action(session, body)
% b  T, ]2 C- l% V* l    def add_fixed_ip(self, session, network_id):
2 d$ ~! r- S$ L        body = {"addFixedIp": {"networkId": network_id}}
9 ]& ^! ~: z, L/ @$ Q        self._action(session, body)
) h, Y- U4 v7 L' A1 O# A8 F! j    def remove_fixed_ip(self, session, address):; i5 o% r, F. h
        body = {"removeFixedIp": {"address": address}}: r" B" f9 F, Q% i7 S( {# H: _
        self._action(session, body)# }6 ^  J8 E3 \% B$ N8 s& D
    def add_floating_ip(self, session, address, fixed_address=None):  T6 l- s3 @, A) ~! n- [2 u& i6 T& O2 k
        body = {"addFloatingIp": {"address": address}}
" R2 I: i! C9 z( ^! n7 b        if fixed_address is not None:
" A" R% E& Y7 V; W, A            body['addFloatingIp']['fixed_address'] = fixed_address
3 U4 y6 x8 G+ u! }        self._action(session, body)
, V3 u% T8 d3 a3 J1 b" F5 A) ?4 A    def remove_floating_ip(self, session, address):
3 J& [  W# |  `4 `        body = {"removeFloatingIp": {"address": address}}
, j! K4 H+ o: M. ~3 Q% h0 \        self._action(session, body)
9 K- W+ O( l0 K% J% W0 J    def backup(self, session, name, backup_type, rotation):
" [  t4 ]' `( X1 V) C        body = {# V- l/ e, Q) e' \6 L! B  L
            "createBackup": {
  A6 B3 t: O' P9 ?+ `  Q" F                "name": name,
& X* ]4 O. E( f+ I+ J                "backup_type": backup_type,/ @8 Y1 p3 m, Y# C+ g) d# Q; I- y$ {
                "rotation": rotation) P- Q% F3 A! H# y
            }8 @: M8 P# r' s: f/ C  v# n6 E2 N
        }
, m0 r( j# z; n6 H# ]3 w" H" B        self._action(session, body)% h% ]0 ~2 S7 a: k/ c
    def pause(self, session):. p+ n) ~; x7 u& c! F8 ^
        body = {"pause": None}
3 e" x2 e! |# \' F- G" g        self._action(session, body)0 H% ]* Z/ W5 H' S/ \  o$ f2 D
    def unpause(self, session):* K0 K6 z$ U$ {4 z+ P
        body = {"unpause": None}' O6 ~/ Y2 F  x' a0 t
        self._action(session, body)0 O( E" I  p$ P. |/ ~* \- ]
    def suspend(self, session):
( @# C5 u8 o, }* i        body = {"suspend": None}
# r, A4 ~, b9 X        self._action(session, body)
( m+ D7 i* {! _" g) ~# ~    def resume(self, session):
7 H( ^* C, x; t( m' A' a% D        body = {"resume": None}
6 u) I0 B, T$ S% @* }. r; E( R        self._action(session, body)- F0 X8 h2 E) j8 s% G
    def lock(self, session):
9 z5 @- `$ B0 X4 J6 i1 S        body = {"lock": None}4 C* v/ m5 m  {& Y5 z% `
        self._action(session, body)
1 {  c0 Z" ?& D# [, C1 Q" O4 v    def unlock(self, session):3 @, `( U7 J# H3 Y  N
        body = {"unlock": None}/ U( v0 W- Y( p& g7 S. n7 V! n
        self._action(session, body)
% Q+ G- \/ e: b* {; r    def rescue(self, session, admin_pass=None, image_ref=None):
4 r/ X$ d# h9 ]" P& h& ~        body = {"rescue": {}}( E4 g; {* w- v. i8 A9 h: i
        if admin_pass is not None:
# `1 m* H5 d, B! n7 a            body["rescue"]["adminPass"] = admin_pass
% j5 A4 u& |  c# W        if image_ref is not None:
7 K0 N: d' f0 n3 S% w; V            body["rescue"]["rescue_image_ref"] = image_ref4 G! W. c! g8 M/ {( V6 p* U) A: X
        self._action(session, body)
  W! [& o4 S2 V! W    def unrescue(self, session):
4 {. Z% G0 t5 x! h3 H2 X) S7 y8 ?        body = {"unrescue": None}
: m3 R  H$ V- }        self._action(session, body)& f! C: N9 t7 ^. b- b* l! Q$ T' O
    def evacuate(self, session, host=None, admin_pass=None, force=None):
- o: H  J" O6 N" M! E# w6 \; _        body = {"evacuate": {}}
5 I/ l- K, f# P- D. a1 a7 F        if host is not None:4 T4 N! _+ {  L8 a' s* V: O4 Z
            body["evacuate"]["host"] = host
: W& r+ {* d  d        if admin_pass is not None:( O. L) `# ~; T9 f  D& l2 o
            body["evacuate"]["adminPass"] = admin_pass
6 o! h% z, c# J. A        if force is not None:' P9 W2 z7 E& \9 Z
            body["evacuate"]["force"] = force3 V% }# O# Q6 J; k
        self._action(session, body)
; T+ C" M7 l0 P' P    def start(self, session):) C# f5 Z; Y# p+ ~5 p! e. c+ ?( q
        body = {"os-start": None}
5 B. Y2 K! A+ u+ G/ e7 R- y        self._action(session, body)
2 y/ s# H' w! ]( n- C    def stop(self, session):; L$ B" M: a9 q' O& i' ^( F% T
        body = {"os-stop": None}
. k0 z; U' e9 q# c. Y  Y! p        self._action(session, body)5 |+ o! r4 b* ^8 [: T8 e/ u! h
    def shelve(self, session):
) K9 L. K  f% }        body = {"shelve": None}
5 K" L' W7 p4 M        self._action(session, body), V, h% E2 `1 D. e! ?8 P* j
    def unshelve(self, session):% m2 A/ i% g, S5 b/ h- }
        body = {"unshelve": None}! I- j5 u/ n3 T  h. r
        self._action(session, body)
# j; w7 Y; _9 W    def migrate(self, session):
! Y8 Z/ r: C7 H! b" V' b        body = {"migrate": None}/ L5 f0 s; S/ B8 U; N4 c3 Q& i7 e4 \
        self._action(session, body)0 }* z# w# l9 s0 g8 P  P
    def get_console_output(self, session, length=None):
) H2 b1 U" [5 _% Z- G        body = {"os-getConsoleOutput": {}}
8 W: u+ g& D, S* w/ k        if length is not None:
; R3 G8 j3 J2 i9 A            body["os-getConsoleOutput"]["length"] = length
) P8 }# B  v& C; E: h        resp = self._action(session, body), \8 X4 S* g: z" R6 q4 d" x
        return resp.json(), |  S7 ^: p: y
    def get_console_url(self, session, console_type):8 V, j" x! n4 c' E
        action = CONSOLE_TYPE_ACTION_MAPPING.get(console_type)
; l/ `. [! |6 ~$ Q2 x6 c        if not action:
: b: ^; Z3 o& V  w            raise ValueError("Unsupported console type %s" % console_type)
* K" d! ~& C: c6 I. B1 q        body = {action: {'type': console_type}}
* J! b2 i) A% x( U4 I% C        resp = self._action(session, body)3 q, r  V, e8 g# F1 P
        return resp.json().get('console')
0 g7 X0 T2 l+ s) ?( Z8 t/ ]% K    def live_migrate(self, session, host, force, block_migration,2 E6 H1 n/ G8 \1 N0 X
                     disk_over_commit=False):
, x5 r! u  E) X( S7 b        if utils.supports_microversion(session, '2.30'):  e3 ^8 o1 b2 z; g. D; H: }  N
            return self._live_migrate_30(
1 Z' f9 |, e. t  y0 \" [' ~8 \                session, host,* p, o+ D! g( A3 m, f
                force=force,
5 A2 w- v0 Z) f5 k$ K. S) y, U                block_migration=block_migration)% }' Y% n3 _6 M* m# N. l, W; z8 |
        elif utils.supports_microversion(session, '2.25'):/ u5 D0 z# B2 o" M8 s
            return self._live_migrate_25(  I- \) d: @' d% b
                session, host,* D4 O4 z. K. C7 n5 d; d# \$ u( W) G0 I
                force=force,
/ U  ]% p7 f: G/ ~# i- \, J                block_migration=block_migration)
+ ~+ P% u/ w8 |0 H# s' n% l( T5 ^        else:
7 o0 i7 L7 O9 j3 ]  q            return self._live_migrate(6 Z4 p$ I: M# v! ?) V/ U' p( Q3 v
                session, host,
4 `. F# x7 o; E* ~" h1 Z                force=force,
2 }2 b. u) T* w) P                block_migration=block_migration,5 H' s( N. a1 ~- I5 X5 R! P. v
                disk_over_commit=disk_over_commit)% k% }7 K9 q% a, h, B
    def _live_migrate_30(self, session, host, force, block_migration):& h: r) v# a8 h9 m$ @9 ?: ^
        microversion = '2.30'
, Q+ J' p" I+ p' O8 ?; d* R( f        body = {'host': None}
" g7 C& n$ e( ]0 X- {7 Z1 t        if block_migration is None:
8 N$ f% j+ _$ |9 ~9 [            block_migration = 'auto'
. F% S' j( Z4 e! l( x        body['block_migration'] = block_migration2 I' ~  b: I3 A* W: R9 z2 i% h" O9 H
        if host:
1 n8 Y2 l) ?0 i3 G  m            body['host'] = host
( @. ?/ ~( P) a& }0 t. n, Y            if force:
; |! Q8 ~. V# l1 R7 @) E! k6 \                body['force'] = force+ C2 b) C9 x( V5 `
        self._action(
7 c8 {- T$ H9 Y$ j3 U0 v            session, {'os-migrateLive': body}, microversion=microversion)" ], p8 V" l9 d6 r" u( w* V- x- s
    def _live_migrate_25(self, session, host, force, block_migration):
' V6 {# e# A& G: W1 w; v        microversion = '2.25'
4 k  C5 j# m3 p. q/ X, n. C        body = {'host': None}% F  O. r0 W4 I3 s$ T+ Q
        if block_migration is None:  i2 K8 G1 M# g! h" K
            block_migration = 'auto'
/ v. z' w4 L& y/ r& w        body['block_migration'] = block_migration0 B" h1 g6 a; T9 v+ Q
        if host:+ d3 e0 B; H) c- g
            body['host'] = host
  A3 g& L' a, E( C, d            if not force:
4 J7 u& ^6 @* u+ H/ \8 M$ A                raise ValueError(
( m3 V& J3 Y& P                    "Live migration on this cloud implies 'force'"3 @, Y( C/ u! a1 y# X5 g
                    " if the 'host' option has been given and it is not"7 @6 I  K/ g. Y' L8 c- J1 B) U9 N
                    " possible to disable. It is recommended to not use 'host'"$ r& M1 ]1 C, ?/ {+ N1 C
                    " at all on this cloud as it is inherently unsafe, but if"3 ~2 V3 w# D9 b
                    " it is unavoidable, please supply 'force=True' so that it"% p' {; f! x, ^
                    " is clear you understand the risks.")
& J& A) B9 z+ f        self._action(
2 [7 s/ p" |9 G' ^2 `& _            session, {'os-migrateLive': body}, microversion=microversion)
( M+ `5 R0 G2 Q; w" m$ f    def _live_migrate(self, session, host, force, block_migration,, v* j7 q1 M2 l4 ?
                      disk_over_commit):- _6 m: |/ S. {  M$ B) p
        microversion = None* y  P( f& w& E- B( [; P+ U( [, n
        body = {
: V. j* r/ y+ Q4 _' b. N+ D            'host': None,, y( H& D3 c7 F  J, g' `
        }5 v) u. P, U$ X2 j
        if block_migration == 'auto':$ ]) W$ s2 y7 O6 i0 s, _. R
            raise ValueError(
" m/ |  e6 V# n  h! J( v/ I% K6 K                "Live migration on this cloud does not support 'auto' as"
" w. \$ b. r" X5 U& T/ C                " a parameter to block_migration, but only True and False.")% U4 Z1 R- |% c; M8 Q! c5 }0 R
        body['block_migration'] = block_migration or False
# B7 t7 \$ f/ J) L- t4 n9 w        body['disk_over_commit'] = disk_over_commit or False: P! r6 N+ O- ~! P2 l) W  t
        if host:
, T2 e. W' t5 p* H            body['host'] = host6 a- e8 t( x% X9 l/ @2 I9 w
            if not force:  c7 _( v) h2 o6 t. @$ }
                raise ValueError(/ [8 e6 ?0 x- K6 w) }6 H
                    "Live migration on this cloud implies 'force'"- I' i+ ]5 N, N. p
                    " if the 'host' option has been given and it is not"
/ R  k9 _, Z6 z, M8 q- K                    " possible to disable. It is recommended to not use 'host'"5 j! W: ?2 o# g$ Y: N' z  u* n
                    " at all on this cloud as it is inherently unsafe, but if"
0 H) t- Y) m4 g7 h2 P                    " it is unavoidable, please supply 'force=True' so that it"' e$ ]' J  F) `! R& V* z
                    " is clear you understand the risks.")
) y9 k3 b. R9 {. E        self._action(
/ n0 _& i( E; j6 ]            session, {'os-migrateLive': body}, microversion=microversion)2 c7 y2 v( K5 f6 ^9 Y0 J
    def fetch_security_groups(self, session):
. z' A/ B* g: r  X5 z# t; D  k        """Fetch security groups of a server.
& N$ O5 P: }( {" f2 w- y2 l        :returns: Updated Server instance.
! D; ~8 x0 @3 f9 N% O4 a7 F        """
- b5 m! x  c# ^& u! ]        url = utils.urljoin(Server.base_path, self.id, 'os-security-groups')
$ ?3 q9 X! v- E' e        response = session.get(url)
+ u/ t2 N; Y1 D( d. G$ Z+ p) X        exceptions.raise_from_response(response). |% n+ |9 r( P2 C% P3 D
        try:6 V; o5 c' y6 q  M' _
            data = response.json()
4 |* g: L! u( S( b' K, ^            if 'security_groups' in data:  K3 D, q8 w% r4 j
                self.security_groups = data['security_groups']/ z4 z, c0 O* X6 i4 ^
        except ValueError:
( z3 W, U9 D- o0 s$ x9 Z; c            pass
$ S+ P4 v, W3 N+ F        return self
1 r: x8 I4 N6 ~8 A8 G  ^! ^( u" |! ~
ServerDetail = Server
( d" V/ E$ p: Q4 c
1 S  x' ^! Z* z: T9 ~+ ~% M9 f" U- Y* ]" S2 D
 楼主| 发表于 2021-9-3 15:49:26 | 显示全部楼层
openstack.tar.gz (1.68 MB, 下载次数: 0, 售价: 5 金钱)
 楼主| 发表于 2021-9-3 15:52:54 | 显示全部楼层
ansible task
6 o+ x# `. D  A4 i/ W7 d/ K% U% iansible-playbook xxxx.yml8 A+ ?2 g, P) d" \, r& n

instence.tar.gz

2.22 KB, 下载次数: 0

售价: 5 金钱  [记录]  [购买]

您需要登录后才可以回帖 登录 | 开始注册

本版积分规则

关闭

站长推荐上一条 /4 下一条

如有购买积分卡请联系497906712

QQ|返回首页|Archiver|手机版|小黑屋|易陆发现 点击这里给我发消息

GMT+8, 2021-11-28 20:39 , Processed in 0.071253 second(s), 25 queries .

Powered by LR.LINUX.cloud bbs168x X3.2 Licensed

© 2012-2022 Comsenz Inc.

快速回复 返回顶部 返回列表