def stopInstance(name, config): """Run the aws command to terminate the instance.""" cmd = [ "aws", "ec2", "terminate-instances", "--instance-ids", _getInstanceIdByName(name) ] common._run_or_none(cmd)
def stopInstance(name, config): """Run the gcloud command to terminate the instance.""" zone = config.get("worker", "zone") cmd = [ "gcloud", "compute", "instances", "delete", "--quiet", "--zone", zone, name ] common._run_or_none(cmd)
def startInstance(name, config): """ Run the gcloud command to start a worker instance. Return the created FIXME """ # gcloud command line tool is picky with params escaping (eg. key-id in json) # So we use a real temporary file startup_script_file = tempfile.NamedTemporaryFile(delete=False) startup_script_file.write(_getStartupScript(name, config)) startup_script_file.flush() os.fsync(startup_script_file.fileno()) cmd = [ "gcloud", "compute", "--project", config.get("authentication", "project"), "instances", "create", name, "--zone", config.get("worker", "zone"), "--machine-type", config.get("worker", "machinetype"), "--subnet", config.get("worker", "subnet"), "--maintenance-policy", config.get("worker", "maintenancepolicy"), "--service-account", config.get("authentication", "serviceaccount"), "--scopes", config.get("authentication", "scopes"), "--image", config.get("worker", "image"), "--image-project", config.get("worker", "imageproject"), "--boot-disk-size", config.get("worker", "bootdisksize"), "--boot-disk-type", config.get("worker", "bootdisktype"), "--boot-disk-device-name", name, "--metadata-from-file", "startup-script={}".format(startup_script_file.name), ] if config.getboolean("worker", "preemptible") == True: cmd.append("--preemptible") common._run_or_none(cmd)
def startInstance(name, config): """ Run the aws command to start a worker instance. Return the created instanceid in case of dedicated ec2 instance or the spotinstancerequestid in case of a spot instance. """ if config.get("worker", "spot"): cmd = [ "aws", "ec2", "request-spot-instances", "--spot-price", config.get("spot", "spotprice"), "--instance-count", config.get("spot", "instancecount"), "--type", config.get("spot", "type"), "--launch-specification", _getLaunchSpecification(name, config), ] else: cmd = [ "aws", "ec2", "run-instances", "--key-name", config.get("authentication", "keyname"), "--image-id", config.get("worker", "imageid"), "--instance-type", config.get("worker", "instancetype"), "--subnet-id", config.get("worker", "subnetid"), "--security-group-ids", config.get("worker", "securitygroupid"), "--iam-instance-profile", "Arn=%s" % config.get("worker", "iaminstanceprofile"), "--user-data", _getUserData(name, config), ] common._run_or_none(cmd)
def _getInstanceIdByName(name): """Return instanceid from name.""" cmd = ["aws", "ec2", "describe-instances"] output = common._run_or_none(cmd) if output: for resources in json.loads(output)["Reservations"]: instance = resources["Instances"][0] if instance.has_key("Tags"): for tags in instance["Tags"]: if tags["Key"] == "Name" and tags["Value"] == name: return instance["InstanceId"] return None
def startInstance(name, config): """ Run the aws command to start a worker instance. Return the created instanceid. """ cmd = ["aws", "ec2", "run-instances", "--key-name", config.get("authentication", "keyname"), "--image-id", config.get("worker", "imageid"), "--instance-type", config.get("worker", "instancetype"), "--subnet-id", config.get("worker", "subnetid"), "--security-group-ids", config.get("worker", "securitygroupid"), "--iam-instance-profile", config.get("worker", "iaminstanceprofile"), "--user-data", _getUserData(name, config),] output = common._run_or_none(cmd) if output: instanceid = json.loads(output)['Instances'][0]['InstanceId'] _nameInstance(instanceid, name) return instanceid return None
def stopInstance(name): """Run the aws command to terminate the instance.""" cmd = ["aws", "ec2", "terminate-instances", "--instance-ids", _getInstanceIdByName(name)] return common._run_or_none(cmd)
def _nameInstance(instanceid, name): """Set the instance name via tag.""" cmd = ["aws", "ec2", "create-tags", "--resources", instanceid, "--tags", "Key=Name,Value=%s" % name] return common._run_or_none(cmd)