def _Exists(self): """Returns true if the VM exists.""" describe_cmd = util.SoftLayer_PREFIX + [ '--format', 'json', 'vs', 'list', '--columns', 'id', '--sortby', 'id', '--domain', 'perfkit.org' ] found = False stdout, _ = util.IssueRetryableCommand(describe_cmd) response = json.loads(stdout) for system in response: if system['id'] == self.id: found = True break if found is False: return False describe_cmd = util.SoftLayer_PREFIX + [ '--format', 'json', 'vs', 'detail', '%s' % self.id ] try: stdout, _ = util.IssueRetryableCommand(describe_cmd) response = json.loads(stdout) status = response['status'] active_transaction = response['active_transaction'] if active_transaction in INSTANCE_QUIESCING_TRANSACTION: return False return status in INSTANCE_EXISTS_STATUSES except errors.VmUtil.CalledProcessException: return False
def ImportKeyfile(self): """Imports the public keyfile to SoftLayer.""" with self._lock: if SoftLayerVirtualMachine.imported_keyfile: return import_cmd = util.SoftLayer_PREFIX + [ 'sshkey', 'add', '--in-file', vm_util.GetPublicKeyPath(), self.key_label ] util.IssueRetryableCommand(import_cmd) SoftLayerVirtualMachine.imported_keyfile = True
def _GetDecodedPasswordData(self): """Get the Password of the VM""" get_password_cmd = util.SoftLayer_PREFIX + [ '--format', 'json', 'vs', 'credentials', '%s' % self.id ] stdout, _ = util.IssueRetryableCommand(get_password_cmd) response = json.loads(stdout) password_data = response[0]['password'] # if no data retry. if not password_data: raise ValueError('No PasswordData in response.') return password_data
def DeleteKeyfile(self): """Deletes the public keyfile to SoftLayer.""" with self._lock: if SoftLayerVirtualMachine.deleted_keyfile: return delete_cmd = util.SoftLayer_PREFIX + [ '-y', '--format', 'json', 'sshkey', 'remove', self.key_label ] util.IssueRetryableCommand(delete_cmd) SoftLayerVirtualMachine.deleted_keyfile = True if SoftLayerVirtualMachine.imported_keyfile: SoftLayerVirtualMachine.imported_keyfile = False
def _PostCreate(self): """Check the status of the system to ensure that it is active Add a non-root user""" describe_cmd = util.SoftLayer_PREFIX + [ '--format', 'json', 'vs', 'detail', '%s' % self.id ] stdout, _ = util.IssueRetryableCommand(describe_cmd) response = json.loads(stdout) transaction = response['active_transaction'] # if not ready raise exception so _PostCreate is called again if transaction is not None: log_msg = ("Post create check for instance %s. Not ready." " Active transaction in progress: %s." % (self.id, transaction)) logging.info(log_msg) sleep(20) raise Exception self.internal_ip = response['private_ip'] self.ip_address = response['public_ip'] domain = response['domain'] logging.info('domain name use is %s' % (domain)) if self.ip_address is None: logging.error('Did not find an IP address') raise Exception util.AddDefaultTags(self.id, self.zone) if (domain != util.defaultDomain): #CPOVRB insert name into NS and wait 30 seconds to ensure activation of name logging.info("reached DNS insert Point for IP: %s." % (self.ip_address)) dnsbld_cmd = util.SoftLayer_PREFIX + [ 'dns', 'record-add', '%s' % domain, '%s' % self.hostname, 'A', '%s' % self.ip_address ] logging.info(dnsbld_cmd) stdout, _, _ = vm_util.IssueCommand(dnsbld_cmd) #CPOVRB #CPOMRS Check to see if DNS addition is working #DNS should not be used unless needed as it will cause an aditional 15minutes loadtime. dnsgood = False while (not dnsgood): hostcmd = ['host', self.hostname + '.' + domain] stdin, _, _ = vm_util.IssueCommand(hostcmd) if "NXDOMAIN" not in stdin: logging.info('DNS working') dnsgood = True else: logging.info('DNS failed retrying in 60 seconds') sleep(60) #CPOMRS # CPOMRS - Delete 127.0.1.1 hosts entry self.RemoteCommand('sed -i \'/127.0.1.1/d\' /etc/hosts') # CPOMRS # Add user and move the key to the non root user if self.user_name != "root": self.user_name = "root" self.RemoteCommand('useradd -m %s' % FLAGS.softlayer_user_name) self.RemoteCommand( 'echo "%s ALL=(ALL:ALL) NOPASSWD: ALL" >> /etc/sudoers' % FLAGS.softlayer_user_name) self.RemoteCommand('mkdir /home/%s/.ssh' % FLAGS.softlayer_user_name) self.RemoteCommand('chmod 700 /home/%s/.ssh/' % FLAGS.softlayer_user_name) self.RemoteCommand('cp ~/.ssh/* /home/%s/.ssh/' % FLAGS.softlayer_user_name) self.RemoteCommand( 'cp /root/.ssh/authorized_keys /home/%s/authorized_keys' % FLAGS.softlayer_user_name) self.RemoteCommand( 'chown -R %s /home/%s/' % (FLAGS.softlayer_user_name, FLAGS.softlayer_user_name)) self.RemoteCommand('rm -rf /root/.ssh') self.user_name = FLAGS.softlayer_user_name