def dns_sync(client, args): from SoftLayer.DNS import DNSManager, DNSZoneNotFound dns = DNSManager(client) cci = CCIManager(client) def sync_a_record(): #hostname = instance['fullyQualifiedDomainName'] records = dns.search_record( instance['domain'], instance['hostname'], ) if not records: # don't have a record, lets add one to the base zone dns.create_record( zone['id'], instance['hostname'], 'a', instance['primaryIpAddress'], ttl=7200) else: recs = filter(lambda x: x['type'].lower() == 'a', records) if len(recs) != 1: raise CLIAbort("Aborting A record sync, found %d " "A record exists!" % len(recs)) rec = recs[0] rec['data'] = instance['primaryIpAddress'] dns.edit_record(rec) def sync_ptr_record(): host_rec = instance['primaryIpAddress'].split('.')[-1] ptr_domains = client['Virtual_Guest'].\ getReverseDomainRecords(id=instance['id'])[0] edit_ptr = None for ptr in ptr_domains['resourceRecords']: if ptr['host'] == host_rec: edit_ptr = ptr break if edit_ptr: edit_ptr['data'] = instance['fullyQualifiedDomainName'] dns.edit_record(edit_ptr) else: dns.create_record( ptr_domains['id'], host_rec, 'ptr', instance['fullyQualifiedDomainName'], ttl=7200) cci_id = resolve_id(cci, args.get('<identifier>')) instance = cci.get_instance(cci_id) if not instance['primaryIpAddress']: raise CLIAbort('No primary IP address associated with this CCI') try: zone = dns.get_zone(instance['domain']) except DNSZoneNotFound: raise CLIAbort("Unable to create A record, " "no zone found matching: %s" % instance['domain']) go_for_it = args['--really'] or confirm( "Attempt to update DNS records for %s" % instance['fullyQualifiedDomainName']) if not go_for_it: raise CLIAbort("Aborting DNS sync") both = False if not args.get('--PTR') and not args.get('-A'): both = True if both or args.get('-A'): sync_a_record() if both or args.get('--PTR'): sync_ptr_record()
class CCIPlatform(Platform): _required_opts = ['cores', 'memory', 'domain', 'datacenter', 'os_code'] def _on_init(self): self._client = Client(username=getenv('SL_USERNAME'), api_key=getenv('SL_API_KEY')) self._manager = CCIManager(self._client) def find_instance(self, host_name): instance = None host_name = host_name.lower() for ii in self._manager.list_instances(): fqdn = ii.get('fullyQualifiedDomainName', '') if fqdn.lower() == host_name: instance = Instance(id=ii.get('id'), name=fqdn) break return instance def get_instance(self, id): cci = self._manager.get_instance(id) return self._cci_to_instance(cci) def create_instance(self, host_name): host_bits = host_name.split('.', 1) host_name = host_bits[0] domain = host_bits[1] if len(host_bits) >= 2 else self.config('domain') base_options = {'cpus': self.config('cores'), 'memory': self.config('memory'), 'hostname': host_name, 'domain': domain, 'datacenter': self.config('datacenter'), 'os_code': self.config('os_code')} print 'creating cci %s/%s' % (host_name, domain) print base_options cci = self._manager.create_instance(**base_options) cci = self._cci_await_ready(cci) self._cci_install_keys(cci['id']) return self._cci_to_instance(cci) def reimage_instance(self, instance): self._manager.reload_instance(instance.id) cci = self._manager.get_instance(instance.id) cci = self._cci_await_transaction_start(cci) cci = self._cci_await_ready(cci) self._cci_install_keys(cci['id']) return self._cci_to_instance(cci) def delete_instance(self, instance): self._manager.cancel_instance(instance.id) self._cci_await_delete(self._manager.get_instance(instance.id)) def instance_ready(self, instance): cci = self._manager.get_instance(instance.id) return (cci and 'activeTransaction' not in cci) def _cci_to_instance(self, cci): if not cci: return None return Instance(id=cci['id'], name=cci['fullyQualifiedDomainName']) def _cci_await_state(self, cci, state_check, sleep_secs=5): wait_start = time() self.log_info('Waiting for %s to change state...' % (cci['id'])) while state_check(cci): sleep(sleep_secs) cci = self._manager.get_instance(cci['id']) self.log_info('...') self.log_info('Available after %0.3f secs.' % (time() - wait_start)) return cci def _cci_await_ready(self, cci): return self._cci_await_state(cci, lambda c: 'activeTransaction' in c, sleep_secs=5) def _cci_await_transaction_start(self, cci): return self._cci_await_state(cci, lambda c: 'activeTransaction' not in c, sleep_secs=2) def _cci_await_delete(self, cci): return self._cci_await_state(cci, lambda c: c and 'id' in c, sleep_secs=2) def _get_cci_root_password(self, cci): passwords = self._manager.get_instance_passwords(cci['id']) password = None for p in passwords: if 'username' in p and p['username'] == 'root': password = p['password'] break return password def _cci_install_keys(self, id): cci = self._manager.get_instance(id) password = self._get_cci_root_password(cci) if not password: raise Exception('Passwords are not available for instance %s' % cci['id']) keys_url = self.config('ssh_key_url') if not keys_url: return client_settings = {'hostname': cci['primaryIpAddress'], 'username': '******', 'password': password} client = SSHClient() client.set_missing_host_key_policy(_SuppressPolicy()) client.connect(look_for_keys=False, **client_settings) client.exec_command('mkdir -p ~/.ssh') client.exec_command('wget -T 10 -q -O ~/.ssh/authorized_keys %s' % keys_url) client.close()
def execute(client, args): cci = CCIManager(client) t = Table(['Name', 'Value']) t.align['Name'] = 'r' t.align['Value'] = 'l' cci_id = resolve_id(cci, args.get('<identifier>')) result = cci.get_instance(cci_id) result = NestedDict(result) t.add_row(['id', result['id']]) t.add_row(['hostname', result['fullyQualifiedDomainName']]) t.add_row(['status', result['status']['name']]) t.add_row(['state', result['powerState']['name']]) t.add_row(['datacenter', result['datacenter'].get('name', blank())]) t.add_row(['cores', result['maxCpu']]) t.add_row(['memory', mb_to_gb(result['maxMemory'])]) t.add_row(['public_ip', result.get('primaryIpAddress', blank())]) t.add_row( ['private_ip', result.get('primaryBackendIpAddress', blank())]) t.add_row([ 'os', FormattedItem( result['operatingSystem']['softwareLicense'] ['softwareDescription'].get('referenceCode', blank()), result['operatingSystem']['softwareLicense'] ['softwareDescription'].get('name', blank()) )]) t.add_row(['private_only', result['privateNetworkOnlyFlag']]) t.add_row(['private_cpu', result['dedicatedAccountHostOnlyFlag']]) t.add_row(['created', result['createDate']]) t.add_row(['modified', result['modifyDate']]) if result.get('notes'): t.add_row(['notes', result['notes']]) if args.get('--price'): t.add_row(['price rate', result['billingItem']['recurringFee']]) if args.get('--passwords'): user_strs = [] for item in result['operatingSystem']['passwords']: user_strs.append( "%s %s" % (item['username'], item['password'])) t.add_row(['users', listing(user_strs)]) tag_row = [] for tag in result['tagReferences']: tag_row.append(tag['tag']['name']) if tag_row: t.add_row(['tags', listing(tag_row, separator=',')]) ptr_domains = client['Virtual_Guest'].\ getReverseDomainRecords(id=cci_id) for ptr_domain in ptr_domains: for ptr in ptr_domain['resourceRecords']: t.add_row(['ptr', ptr['data']]) return t