def start_instance(self, instance_id): status = self.get_instance_status_by_id(instance_id) if status != ECS_STATUS_STOPPED: logger.warning("instance [%s] is not in [%s],current status [%s], cannot start it", instance_id, ",".join([ECS_STATUS_STOPPED]), status) return None request = StartInstanceRequest() request.set_InstanceId(instance_id) response = self.execute(request) return response
def start_instance(self, instanceid): """ 启动实例 :param instanceid: :return: """ client = AcsClient(self.AccessKeyId, self.AccessKeySecret, instanceid) request = StartInstanceRequest() request.set_accept_format('json') response = client.do_action_with_exception(request) res = json.loads(str(response, encoding='utf-8')) return res
def start_instance(self, instance_id): """ Start an ECS instance. :param instance_id: The Ecs instance ID. """ request = StartInstanceRequest() request.set_InstanceId(instance_id) response = self._send_request(request) if response is not None: logging.info("instance %s start successfully.", instance_id) else: logging.error("instance %s start failed.", instance_id)
def _boot_image(self, zone_id, image_id, instance_type, vswitch_id, instance_name, ssh_keyname, security_group_id): request = CreateInstanceRequest() request.set_ImageId(image_id) request.set_SecurityGroupId(security_group_id) request.set_InstanceType(instance_type) request.set_VSwitchId(vswitch_id) request.set_ZoneId(zone_id) request.set_InstanceChargeType('PostPaid') request.set_KeyPairName(ssh_keyname) request.set_InternetMaxBandwidthOut(1) response = self._send_request(request) instance_id = response.get('InstanceId') if instance_id is None: logger.info(response) raise Exception("Instance could not be created.") self.instance_id = instance_id logger.info("Instance %s created" % instance_id) logger.info( "Wait for the instance to appear as stopped so we can continue") max_tries = 10 status = None while max_tries > 0 and status != 'Stopped': max_tries -= 1 time.sleep(5) status = self._get_instance_status_by_id(instance_id) if status != 'Stopped': raise Exception("VM %s not in desired state" % instance_id) logger.info("Allocating public ip for instance %s" % instance_id) request = AllocatePublicIpAddressRequest() request.set_InstanceId(instance_id) ip_address = self._send_request(request) logger.info("Public IP address asssigned to instance: %s" % ip_address) logger.info(json.dumps(ip_address, indent=4)) logger.info("Starting instance %s" % instance_id) request = StartInstanceRequest() request.set_InstanceId(instance_id) self._send_request(request) # wait for instance to be started max_tries = 60 status = "initial" while max_tries > 0 and status != 'Running': max_tries -= 1 time.sleep(10) status = self._get_instance_status_by_id(instance_id) logger.info("Status is %s" % status) if status != 'Running': raise Exception("Instance %s status is %s: failed to start." % (instance_id, status)) return (instance_id, ip_address)
def StartInstance(ak, sk, region_tag, instance_id): client = AcsClient(ak, sk, region_tag) request = StartInstanceRequest() request.set_accept_format('json') request.set_InstanceId(instance_id) response = client.do_action_with_exception(request) print(str(response, encoding='utf-8'))
def startInstance(InstanceId): request = StartInstanceRequest() request.set_accept_format('json') request.set_InstanceId(InstanceId) response = client.do_action_with_exception(request) response = json.loads(str(response, encoding='utf-8')) return response
def _start_instance(self): """Start the instance.""" client = self._connect() request = StartInstanceRequest() request.set_accept_format('json') request.set_InstanceId(self.running_instance_id) client.do_action_with_exception(request) self._wait_on_instance('Running', self.timeout)
def start_host(self, instance_id): client = AcsClient(self.access_key_id, self.secret_access_key, self.region_id) request = StartInstanceRequest() request.set_accept_format('json') request.set_InstanceId(instance_id) response = client.do_action_with_exception(request) # python2: print(response) print(str(response, encoding='utf-8'))
def start_instance(self, InstanceId): request = StartInstanceRequest() request.set_accept_format('json') request.set_InstanceId(InstanceId) status = self.check_ecs_status(InstanceId) if not status: try: response = self.__client.do_action_with_exception(request) except Exception as e: print(e) print('%s 实例启动失败' % InstanceId) else: print('%s 实例状态不为Stopping,无法启动!!!!' % InstanceId)
def start_instance(client: AcsClient, instance_id: str): request = StartInstanceRequest() request.set_accept_format('json') request.set_InstanceId(instance_id) try: response = client.do_action_with_exception(request) except (ServerException, ClientException) as e: print("启动失败.") print(e.error_code) raise e print("启动成功! 请等待一分钟左右,服务器即可用。") print("稍等片刻后可以使用status命令获取服务器IP地址。")
def startInstance(self, clt, instanceID): ret = {} request = StartInstanceRequest() request.set_InstanceId(instanceID) request.set_accept_format('json') try: response_str = json.loads(clt.do_action(request)) logger.debug("response: %s" % response_str) if 'Code' in response_str: ret['code'] = response_str['Code'] else: ret['code'] = 0 ret['msg'] = response_str except Exception as e: ret['code'] = 1 ret['msg'] = str(e) return ret
def act_start(self, request, *args, **kwargs): obj = get_object_or_404(ServerConfigModel, pk=kwargs['pk']) _request = StartInstanceRequest() _request.set_accept_format('json') _request.set_InstanceId(obj.instance_id) try: self.acsclient.do_action_with_exception(_request) obj.server_status.running_status = 1 obj.server_status.save() self.message_user(request, 'Start Successful', messages.SUCCESS) except Exception as e: obj.server_status.running_status = 2 obj.server_status.save() self.message_user(request, 'Start Failed: %s' % e.message, messages.ERROR) co_path = request.path.split('/') co_path[-2] = '' return redirect('/'.join(co_path).replace('//', '/'))
def make(): security_group_dict = acdb.db.load( "security_group_dict") # map[region_id]security_group_id region_instance_id_list = [] # Check image id is avaliable for node_conf in conf.config["ecs"]["node"]: print("Get request", node_conf) client = AcsClient( conf.secret["aliyun"]["access_key"], conf.secret["aliyun"]["access_secret"], node_conf["region_id"], ) req = DescribeImagesRequest() req.set_Status("Available") req.set_ImageOwnerAlias("system") req.set_InstanceType(node_conf["instance_type"]) req.set_OSType("linux") req.set_PageSize(100) req.set_ActionType("CreateEcs") res = json.loads(client.do_action_with_exception(req)) all_avaliable_image = [e["ImageId"] for e in res["Images"]["Image"]] assert node_conf["image"] in all_avaliable_image # Create ECS instance for node_conf in conf.config["ecs"]["node"]: client = AcsClient( conf.secret["aliyun"]["access_key"], conf.secret["aliyun"]["access_secret"], node_conf["region_id"], ) req = CreateInstanceRequest() req.set_ImageId(node_conf["image"]) req.set_SecurityGroupId(security_group_dict[node_conf["region_id"]]) req.set_Password(conf.secret["aliyun"]["ecs_password"]) req.set_InstanceChargeType("PostPaid") req.set_SystemDiskCategory("cloud_efficiency") req.set_SystemDiskSize(c_system_disk_size) req.set_InstanceType(node_conf["instance_type"]) req.set_InternetMaxBandwidthOut( node_conf["internet_max_bandwidth_out"]) req.set_KeyPairName(c_keypair_name) res = json.loads(client.do_action_with_exception(req)) instance_id = res["InstanceId"] print("Create", node_conf["region_id"], instance_id) region_instance_id_list.append([node_conf["region_id"], instance_id]) print("Wait") for _ in range(1 << 32): if sum([ e["Status"] == "Stopped" for e in info(region_instance_id_list) ]) == len(region_instance_id_list): break for (region_id, instance_id) in region_instance_id_list: client = AcsClient( conf.secret["aliyun"]["access_key"], conf.secret["aliyun"]["access_secret"], region_id, ) # Allocate public ip address req = AllocatePublicIpAddressRequest() req.set_accept_format("json") req.set_InstanceId(instance_id) client.do_action_with_exception(req) print("Allocate public ip address", region_id, instance_id) # Start ECS instance req = StartInstanceRequest() req.set_accept_format("json") req.set_InstanceId(instance_id) res = json.loads(client.do_action_with_exception(req)) print("Start", region_id, instance_id) print("Wait") for _ in range(1 << 32): if sum([ e["Status"] == "Running" for e in info(region_instance_id_list) ]) == len(region_instance_id_list): break # Get ECS info acdb.db.save("instance_list", info(region_instance_id_list)) print( f"Instance info saved at {conf.config['db']['path']}/instance_list.json" )
from aliyunsdkcore.client import AcsClient from aliyunsdkecs.request.v20140526.StartInstanceRequest import StartInstanceRequest client = AcsClient('access_key_id', 'access_key_secret', 'region_id') request = StartInstanceRequest() request.set_InstanceId('ECS实例ID') response = client.do_action_with_exception(request) print(response)
def startup_ecs(self,instanceid): request = StartInstanceRequest() request.set_InstanceId(instanceid) return request
def start(self): request = StartInstanceRequest() request.set_InstanceId(self.instance_id) _do_request(self._client, request, {})
def Start_Instance(self, instance_id): request = StartInstanceRequest() request.set_InstanceId(instance_id) self._send_request(request)
def start_instance(conn, instance_id): request = StartInstanceRequest() request.set_InstanceId(instance_id) _send_request(conn, request)
def start_instance(conn, instance_id): logging.debug("start instance %s" % instance_id) request = StartInstanceRequest() request.set_InstanceId(instance_id) _send_request(conn, request)