def run_add(self): m = Machine(self.args.machine, user=self.args.username) t = Template(self.args.template, user=self.args.username) # grab the template we're associating to the machine try: t = self.cs.template_get(t, auth=True) except ServiceException as e: print(e) return 1 # add template uuid to machine m.template = t.uuid # add machine bits that are specified if self.args.description is not None: m.description = self.args.description try: res = self.cs.machine_create(m) except ServiceException as e: print(e) return 1 print(res) # update config with our newly added (registered) machine self.config.set('machine', 'uuid', res['uuid']) self.config.set('machine', 'key', res['key']) self.config.save() print('info: machine added.') return 0
def run_add(self): m = Machine(self.args.machine, user=self.args.username) t = Template(self.args.template, user=self.args.username) # grab the template we're associating to the machine try: t = self.cs.template_get(t, auth=True, resolve_includes=False) except ServiceException as e: print(e) return 1 # add template uuid to machine m.template = t.uuid # add machine bits that are specified if self.args.description is not None: m.description = self.args.description try: res = self.cs.machine_create(m) except ServiceException as e: print(e) return 1 print(res) # update config with our newly added (registered) machine self.config.set('machine', 'uuid', res['uuid']) self.config.set('machine', 'key', res['key']) self.config.save() print('info: machine added.') return 0
def run_diff(self): uuid = self.config.get('machine', 'uuid') key = self.config.get('machine', 'key') try: res = self.cs.machine_sync(uuid, key, template=True) except ServiceException as e: print(e) return 1 m = Machine(res['template']) t = Template(res['template']) ts = Template.from_system() (l_r, r_l) = t.package_diff(ts.packages_all) print("In template not in system:") for p in l_r: print(" - {0}".format(p.name)) print() print("On system not in template:") for p in r_l: print(" + {0}".format(p.name)) print()
def test_machine_parse_str_valid(self): # valid m2 = Machine("foo:bar") m3 = Machine("foo:bar@baz") m4 = Machine("foo:bar@") self.assertEqual("foo", m2.user) self.assertEqual("bar", m2.name) self.assertEqual(None, m2.version) self.assertEqual("foo", m3.user) self.assertEqual("bar", m3.name) self.assertEqual("baz", m3.version) self.assertEqual("foo", m4.user) self.assertEqual("bar", m4.name) self.assertEqual(None, m4.version)
def run_sync(self): uuid = self.config.get('machine', 'uuid') key = self.config.get('machine', 'key') try: res = self.cs.machine_sync(uuid, key, template=True) except ServiceException as e: print(e) return 1 m = Machine(res['template']) t = Template(res['template']) t.system_prepare() # describe process for dry runs if self.args.dry_run: tx = t.system_transaction() packages_install = list(tx.install_set) packages_install.sort(key=lambda x: x.name) packages_remove = list(tx.remove_set) packages_remove.sort(key=lambda x: x.name) if len(packages_install) or len(packages_remove): print( 'The following would be installed to (+) and removed from (-) the system:' ) for p in packages_install: print(' + ' + str(p)) for p in packages_remove: print(' - ' + str(p)) print() print('Summary:') print(' - Package(s): %d' % (len(packages_install) + len(packages_remove))) print() else: print('No system changes required.') print('No action peformed during this dry-run.') return 0 # TODO: progress for download, install and removal t.to_system_apply(clean=False) print('info: machine synced.') return 0
def run_rm(self): m = Machine(self.args.machine, user=self.args.username) try: res = self.cs.machine_delete(m) except ServiceException as e: print(e) return 1 self.config.unset('machine', 'uuid') self.config.unset('machine', 'key') self.config.save() print('info: machine removed.') return 0
def machine_get(self, machine): if not isinstance(machine, Machine): TypeError('machine is not of type Machine') query = { 'user': machine.user, 'name': machine.name, 'version': machine.version } query = {k: v for k, v in query.items() if v != None} r = urllib.request.Request('{0}/api/machines.json?{1}'.format( self._urlbase, urllib.parse.urlencode(query))) try: u = self._opener.open(r) machine_summary = json.loads(u.read().decode('utf-8')) # nothing returned, so authenticate and retry if len(machine_summary) == 0 and not self._authenticated: self.authenticate() u = self._opener.open(r) machine_summary = json.loads(u.read().decode('utf-8')) if len(machine_summary): # we only have one returned since machine names are unique per account r = urllib.request.Request('{0}/api/machine/{1}.json'.format( self._urlbase, machine_summary[0]['uuid'])) u = self._opener.open(r) data = json.loads(u.read().decode('utf-8')) return Machine(machine=data) raise ServiceException('unable to get machine') except urllib.error.URLError as e: res = json.loads(e.fp.read().decode('utf-8')) raise ServiceException('{0}'.format(res.get('error', 'unknown'))) except urllib.error.HTTPError as e: logging.debug(e) raise ServiceException('unknown service response')
def run_update(self): m = Machine(self.args.machine, user=self.args.username) try: m = self.cs.machine_get(m) except ServiceException as e: print(e) return 1 # add machine bits that are specified for update if self.args.template: t = Template(self.args.template, user=self.args.username) try: t = self.cs.template_get(t, resolve_includes=False) except ServiceException as e: print(e) return 1 m.template = t.uuid if self.args.name is not None: m.name = self.args.name if self.args.title is not None: m.title = self.args.title if self.args.title is not None: m.title = self.args.title if self.args.description is not None: m.description = self.args.description try: res = self.cs.machine_update(m) except ServiceException as e: print(e) return 1 print('info: machine updated.') return 0
def run_update(self): m = Machine(self.args.machine, user=self.args.username) try: m = self.cs.machine_get(m) except ServiceException as e: print(e) return 1 # add machine bits that are specified for update if self.args.template: t = Template(self.args.template, user=self.args.username) try: t = self.cs.template_get(t) except ServiceException as e: print(e) return 1 m.template = t.uuid if self.args.name is not None: m.name = self.args.name if self.args.title is not None: m.title = self.args.title if self.args.title is not None: m.title = self.args.title if self.args.description is not None: m.description = self.args.description try: res = self.cs.machine_update(m) except ServiceException as e: print(e) return 1 print('info: machine updated.') return 0
def test_machine_parse_str_invalid(self): # empty string with self.assertRaises(ErrorInvalidMachine): Machine("") # pure whitespace with self.assertRaises(ErrorInvalidMachine): Machine(" ") # no user with self.assertRaises(ErrorInvalidMachine): Machine("foo") with self.assertRaises(ErrorInvalidMachine): Machine(":foo@bar") # no user or version with self.assertRaises(ErrorInvalidMachine): Machine(":foo@") # no name with self.assertRaises(ErrorInvalidMachine): Machine("foo:")