Esempio n. 1
0
    def run_rm(self):
        t = Template(self.args.template, user=self.args.username)

        try:
            t = self.cs.template_get(t)

        except ServiceException as e:
            print(e)
            return 1

        for o in self.args.objects:
            try:
                obj = Object(name=o)

            except ErrorInvalidObject as e:
                print (e)
                return 1

            t.remove_object(obj)

        if self.args.dry_run:
            # TODO: print changes
            return 1

        # push our updated template
        try:
            res = self.cs.template_update(t)

        except ServiceException as e:
            print(e)
            return 1

        return 0
Esempio n. 2
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('system')
        ts.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()
Esempio n. 3
0
    def run_add(self):
        t = Template(self.args.template, user=self.args.username)

        try:
            t = self.cs.template_get(t)

        except ServiceException as e:
            print(e)
            return 1

        try:
            obj = Object(name=self.args.object, data=self.args.data, data_file=self.args.data_file, source=self.args.source, xsum=self.args.xsum, actions=self.args.actions)
        except ErrorInvalidObject as e:
            print (e)
            return 1

        t.add_object(obj)

        # describe process for dry runs
        if self.args.dry_run:
            print('The following object would be added to the template: {0}'.format(t.name))

            print('  - ' + str(obj))
            print()
            return 1

        # push our updated template
        try:
            res = self.cs.template_update(t)

        except ServiceException as e:
            print(e)
            return 1

        return 0
Esempio n. 4
0
    def run_update(self):
        t = Template(self.args.template, user=self.args.username)

        try:
            t = self.cs.template_get(t, resolve_includes=False)

        except ServiceException as e:
            logging.exception(e)
            return 1

        # add template bits that are specified for update
        if self.args.title is not None:
            t.title = self.args.title

        if self.args.description is not None:
            t.description = self.args.description

        if self.args.includes is not None:
            t.includes = self.args.includes

        if self.args.public is not None:
            t.public = self.args.public

        try:
            res = self.cs.template_update(t)

        except ServiceException as e:
            logging.exception(e)
            return 1

        logging.info('Template updated.')
        return 0
Esempio n. 5
0
    def run_add(self):
        t = Template(self.args.template, user=self.args.username)

        try:
            t = self.cs.template_get(t)

        except ServiceException as e:
            print(e)
            return 1

        try:
            obj = Object(name=self.args.object, data=self.args.data, data_file=self.args.data_file, source=self.args.source, xsum=self.args.xsum, actions=self.args.actions)
        except ErrorInvalidObject as e:
            print (e)
            return 1

        t.add_object(obj)

        # describe process for dry runs
        if self.args.dry_run:
            print('The following object would be added to the template: {0}'.format(t.name))

            print('  - ' + str(obj))
            print()
            return 1

        # push our updated template
        try:
            res = self.cs.template_update(t)

        except ServiceException as e:
            print(e)
            return 1

        return 0
Esempio n. 6
0
    def run_add(self):
        t = Template(self.args.template, user=self.args.username)

        # add template bits that are specified
        if self.args.title is not None:
            t.title = self.args.title

        if self.args.description is not None:
            t.description = self.args.description

        if self.args.includes is not None:
            t.includes = self.args.includes

        if self.args.public is not None:
            t.public = self.args.public

        try:
            res = self.cs.template_create(t)

        except ServiceException as e:
            logging.exception(e)
            return 1

        logging.info('Template added.')
        return 0
Esempio n. 7
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()
Esempio n. 8
0
    def run_rm(self):
        t = Template(self.args.template, user=self.args.username)

        try:
            t = self.cs.template_get(t)

        except ServiceException as e:
            print(e)
            return 1

        for o in self.args.objects:
            try:
                obj = Object(name=o)

            except ErrorInvalidObject as e:
                print (e)
                return 1

            t.remove_object(obj)

        if self.args.dry_run:
            # TODO: print changes
            return 1

        # push our updated template
        try:
            res = self.cs.template_update(t)

        except ServiceException as e:
            print(e)
            return 1

        return 0
Esempio n. 9
0
    def run_add(self):
        t = Template(self.args.template, user=self.args.username)

        try:
            t = self.cs.template_get(t)

        except ServiceException as e:
            print(e)
            return 1

        for p in self.args.package:
            try:
                pkg = Package(p)
            except TypeError as e:
                print(e)
                return 1

            t.add_package(pkg)

        packages = list(t.packages_delta)
        packages.sort(key=lambda x: x.name)

        # describe process for dry runs
        if self.args.dry_run:
            if len(packages):
                print(
                    'The following would be added to the template: {0}'.format(
                        t.name))

                for p in packages:
                    print('  - ' + str(p))

                print()
                print('Summary:')
                print('  - Package(s): %d' % (len(packages)))
                print()

            else:
                print('No template changes required.')

            print('No action peformed during this dry-run.')
            return 0

        if not len(packages):
            print('info: no changes detected, template up to date.')
            return 0

        # push our updated template
        try:
            res = self.cs.template_update(t)

        except ServiceException as e:
            print(e)
            return 1
Esempio n. 10
0
    def test_template_add_includes_str(self):
        t1 = Template("foo:bar")

        t1.includes = 'foo'
        self.assertEqual(["foo:foo"], t1.includes)

        t1.includes = ['foo', 'bar@baz']
        self.assertEqual(["foo:foo", "foo:bar@baz"], t1.includes)

        t1.includes = 'foo,bar@baz,baz'
        self.assertEqual(["foo:foo", "foo:bar@baz", "foo:baz"], t1.includes)
Esempio n. 11
0
    def test_template_add_includes_str(self):
        t1 = Template("foo:bar")

        t1.includes = 'foo'
        self.assertEqual(["foo:foo"], t1.includes)

        t1.includes = ['foo','bar@baz']
        self.assertEqual(["foo:foo", "foo:bar@baz"], t1.includes)

        t1.includes = 'foo,bar@baz,baz'
        self.assertEqual(["foo:foo", "foo:bar@baz", "foo:baz"], t1.includes)
Esempio n. 12
0
    def run_add(self):
        t = Template(self.args.template, user=self.args.username)

        try:
            t = self.cs.template_get(t)

        except ServiceException as e:
            print(e)
            return 1

        for p in self.args.package:
            try:
                pkg = Package(p)
            except TypeError as e:
                print(e)
                return 1

            t.add_package(pkg)

        packages = list(t.packages_delta)
        packages.sort(key=lambda x: x.name)

        # describe process for dry runs
        if self.args.dry_run:
            if len(packages):
                print("The following would be added to the template: {0}".format(t.name))

                for p in packages:
                    print("  - " + str(p))

                print()
                print("Summary:")
                print("  - Package(s): %d" % (len(packages)))
                print()

            else:
                print("No template changes required.")

            print("No action peformed during this dry-run.")
            return 0

        if not len(packages):
            print("info: no changes detected, template up to date.")
            return 0

        # push our updated template
        try:
            res = self.cs.template_update(t)

        except ServiceException as e:
            print(e)
            return 1
Esempio n. 13
0
    def run_rm(self):
        t = Template(self.args.template, user=self.args.username)

        try:
            t = self.cs.template_get(t)

        except ServiceException as e:
            print(e)
            return 1

        repos = []

        for r in self.args.repo:
            r = Repository(r)
            if t.remove_repo(r):
                repos.append(r)

        repos.sort(key=lambda x: x.stub)

        # describe process for dry runs
        if self.args.dry_run:
            if len(repos):
                print('The following would be removed from the template: {0}'.
                      format(t.name))

                for r in repos:
                    print('  - ' + str(r))

                print()
                print('Summary:')
                print('  - Repo(s): %d' % (len(repos)))
                print()

            else:
                print('No template changes required.')

            print('No action peformed during this dry-run.')
            return 0

        if not len(repos):
            print('info: no changes detected, template up to date.')
            return 0

        # push our updated template
        try:
            res = self.cs.template_update(t)

        except ServiceException as e:
            print(e)
            return 1
Esempio n. 14
0
  def run_update(self):
    t = Template(self.args.template, user=self.args.username)

    try:
      t = self.cs.template_get(t)

    except ServiceException as e:
      print(e)
      return 1

    r = t.find_repo(self.args.repo)

    if len(r) != 1:
      print('error: repo is not defined in template.')
      return 1

    r = r[0]
    o = {
      'n':  self.args.name,
      'bu': self.args.baseurl,
      'ml': self.args.mirrorlist,
      'ma': self.args.metalink,
      'e':  self.args.enabled,
      'c':  self.args.cost,
      'p':  self.args.priority,
      'gc': self.args.gpgcheck,
      'gk': self.args.gpgkey,
      'sk': self.args.skip,
      'x':  self.args.exclude
    }

    o = {k: v for k, v in o.items() if v != None}

    r.parse(o)

    if not t.update_repo(r):
      print('error: no changes detected.')
      return 0

    # push our updated template
    try:
      res = self.cs.template_update(t)

    except ServiceException as e:
      print(e)
      return 1

    print('info: repo updated.')
    return 0
Esempio n. 15
0
  def run_rm(self):
    t = Template(self.args.template, user=self.args.username)

    try:
      t = self.cs.template_get(t)

    except ServiceException as e:
      print(e)
      return 1

    repos = []

    for r in self.args.repo:
      r = Repository(r)
      if t.remove_repo(r):
        repos.append(r)

    repos.sort(key=lambda x: x.stub)

    # describe process for dry runs
    if self.args.dry_run:
      if len(repos):
        print('The following would be removed from the template: {0}'.format(t.name))

        for r in repos:
          print('  - ' + str(r))

        print()
        print('Summary:')
        print('  - Repo(s): %d' % ( len(repos) ))
        print()

      else:
        print('No template changes required.')

      print('No action peformed during this dry-run.')
      return 0

    if not len(repos):
      print('info: no changes detected, template up to date.')
      return 0

    # push our updated template
    try:
      res = self.cs.template_update(t)

    except ServiceException as e:
      print(e)
      return 1
Esempio n. 16
0
    def run_rm(self):
        t = Template(self.args.template, user=self.args.username)

        try:
            t = self.cs.template_get(t)

        except ServiceException as e:
            print(e)
            return 1

        packages = []

        for p in self.args.package:
            p = Package(p)
            if t.remove_package(p):
                packages.append(p)

        packages.sort(key=lambda x: x.name)

        # describe process for dry runs
        if self.args.dry_run:
            if len(packages):
                print("The following would be removed from the template: {0}".format(t.name))

                for p in packages:
                    print("  - " + str(p))

                print()
                print("Summary:")
                print("  - Package(s): %d" % (len(packages)))
                print()

            else:
                print("No template changes required.")

            print("No action peformed during this dry-run.")
            return 0

        if not len(packages):
            print("info: no changes detected, template up to date.")
            return 0

        # push our updated template
        try:
            res = self.cs.template_update(t)

        except ServiceException as e:
            print(e)
            return 1
Esempio n. 17
0
    def run_update(self):
        t = Template(self.args.template, user=self.args.username)

        try:
            t = self.cs.template_get(t)

        except ServiceException as e:
            print(e)
            return 1

        r = t.find_repo(self.args.repo)

        if len(r) != 1:
            print('error: repo is not defined in template.')
            return 1

        r = r[0]
        o = {
            'n': self.args.name,
            'bu': self.args.baseurl,
            'ml': self.args.mirrorlist,
            'ma': self.args.metalink,
            'e': self.args.enabled,
            'c': self.args.cost,
            'p': self.args.priority,
            'gc': self.args.gpgcheck,
            'gk': self.args.gpgkey,
            'sk': self.args.skip,
            'x': self.args.exclude
        }

        o = {k: v for k, v in o.items() if v != None}

        r.parse(o)

        if not t.update_repo(r):
            print('error: no changes detected.')
            return 0

        # push our updated template
        try:
            res = self.cs.template_update(t)

        except ServiceException as e:
            print(e)
            return 1

        print('info: repo updated.')
        return 0
Esempio n. 18
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
Esempio n. 19
0
    def run_diff(self):
        t = Template(self.args.template_from, user=self.args.username)

        # grab the template we're pushing to
        try:
            t = self.cs.template_get(t)

        except ServiceException as e:
            logging.exception(e)
            return 1

        # fetch template to compare to
        if self.args.template_to is not None:
            ts = Template(self.args.template_to, user=self.args.username)

            try:
                ts = self.cs.template_get(ts)

            except ServiceException as e:
                logging.exception(e)
                return 1

        # otherwise build from system
        else:
            ts = Template.from_system()

        (l_r, r_l) = t.package_diff(ts.packages_all)

        if len(l_r):
            print('In template and not marked for install in system:')

            for p in l_r:
                print(" * {0}".format(p.name))

            print()

        if len(r_l):
            print('Marked for install on system and not in template:')

            for p in r_l:
                print(" * {0}".format(p.name))

        print()
Esempio n. 20
0
    def test_template_parse_str_valid(self):
        t1 = Template("foo:bar")
        t2 = Template("foo:bar@baz")
        t3 = Template("foo:bar@")

        self.assertEqual("foo", t1.user)
        self.assertEqual("bar", t1.name)
        self.assertEqual(None, t1.version)
        self.assertEqual("foo:bar", t1.unv)

        self.assertEqual("foo", t2.user)
        self.assertEqual("bar", t2.name)
        self.assertEqual("baz", t2.version)
        self.assertEqual("foo:bar@baz", t2.unv)

        self.assertEqual("foo", t3.user)
        self.assertEqual("bar", t3.name)
        self.assertEqual(None, t3.version)
        self.assertEqual("foo:bar", t3.unv)
Esempio n. 21
0
    def run_diff(self):
        t = Template(self.args.template_from, user=self.args.username)

        # grab the template we're pushing to
        try:
            t = self.cs.template_get(t)

        except ServiceException as e:
            logging.exception(e)
            return 1

        # fetch template to compare to
        if self.args.template_to is not None:
            ts = Template(self.args.template_to, user=self.args.username)

            try:
                ts = self.cs.template_get(ts)

            except ServiceException as e:
                logging.exception(e)
                return 1

        # otherwise build from system
        else:
            ts = Template.from_system()

        (l_r, r_l) = t.package_diff(ts.packages_all)

        if len(l_r):
            print('In template and not marked for install in system:')

            for p in l_r:
                print(" * {0}".format(p.name))

            print()

        if len(r_l):
            print('Marked for install on system and not in template:')

            for p in r_l:
                print(" * {0}".format(p.name))

        print()
Esempio n. 22
0
    def run_update(self):
        t = Template(self.args.template, user=self.args.username)

        try:
            t = self.cs.template_get(t)

        except ServiceException as e:
            print(e)
            return 1

        # track updates to determine server update
        updated = False

        for p in self.args.package:

            # parse new and find old
            pn = Package(p)

            print(pn)

            if pn not in t.packages:
                print('warn: package is not defined in template.')
                continue

            # update with new and track
            if t.update_package(pn):
                updated = True

        if not updated:
            print('info: no changes detected.')
            return 0

        # push our updated template
        try:
            res = self.cs.template_update(t)

        except ServiceException as e:
            print(e)
            return 1

        print('info: package(s) updated.')
        return 0
Esempio n. 23
0
  def run_update(self):
    t = Template(self.args.template, user=self.args.username)

    try:
      t = self.cs.template_get(t)

    except ServiceException as e:
      print(e)
      return 1

    # track updates to determine server update
    updated = False

    for p in self.args.package:

      # parse new and find old
      pn = Package(p)

      print(pn)

      if pn not in t.packages:
        print('warn: package is not defined in template.')
        continue

      # update with new and track
      if t.update_package(pn):
        updated = True

    if not updated:
      print('info: no changes detected.')
      return 0

    # push our updated template
    try:
      res = self.cs.template_update(t)

    except ServiceException as e:
      print(e)
      return 1

    print('info: package(s) updated.')
    return 0
Esempio n. 24
0
    def run_pull(self):
        # am i effectively root
        if not self.args.dry_run and os.geteuid() != 0:
            logging.error(
                'You need to have root privileges to modify the system.')
            return 0

        t = Template(self.args.template, user=self.args.username)

        try:
            t = self.cs.template_get(t)

        except ServiceException as e:
            logging.exception(e)
            return 1

        t.system_prepare(clean=self.args.pull_clean)

        # 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:')

                if len(packages_install):
                    print('  - %d package(s) installed' %
                          (len(packages_install)))

                if len(packages_remove):
                    print('  - %d package(s) removed' % (len(packages_remove)))

                print()

            else:
                print('No system changes required.')

            logging.info('No action peformed during this dry-run.')
            return 0

        t.system_apply()
Esempio n. 25
0
    def run_rm(self):
        t = Template(self.args.template, user=self.args.username)

        try:
            res = self.cs.template_delete(t)

        except ServiceException as e:
            logging.exception(e)
            return 1

        logging.info('Template removed.')
        return 0
Esempio n. 26
0
    def run_rm(self):
        t = Template(self.args.template, user=self.args.username)

        try:
            res = self.cs.template_delete(t)

        except ServiceException as e:
            print(e)
            return 1

        print('info: template removed.')
        return 0
Esempio n. 27
0
    def run_add(self):
        t = Template(self.args.template, user=self.args.username)

        try:
            t = self.cs.template_get(t)

        except ServiceException as e:
            print(e)
            return 1

        # default enabled
        if self.args.enabled is None:
            self.args.enabled = True

        r = Repository({
            'stub'       : self.args.repo,
            'name'       : self.args.name,
            'baseurl'    : self.args.baseurl,
            'mirrorlist' : self.args.mirrorlist,
            'metalink'   : self.args.metalink,
            'enabled'    : self.args.enabled,
            'cost'       : self.args.cost,
            'priority'   : self.args.priority,
            'gpgkey'     : self.args.gpgkey,
            'gpgcheck'   : self.args.gpgcheck,
            'exclude'    : self.args.exclude
        })

        t.add_repo(r)

        # push our updated template
        try:
            res = self.cs.template_update(t)

        except ServiceException as e:
            print(e)
            return 1

        print('info: repo added.')
        return 0
Esempio n. 28
0
    def run_update(self):
        t = Template(self.args.template, user=self.args.username)

        try:
            t = self.cs.template_get(t, resolve_includes=False)

        except ServiceException as e:
            logging.exception(e)
            return 1

        # add template bits that are specified for update
        if self.args.title is not None:
            t.title = self.args.title

        if self.args.description is not None:
            t.description = self.args.description

        if self.args.includes is not None:
            t.includes = self.args.includes

        if self.args.public is not None:
            t.public = self.args.public

        try:
            res = self.cs.template_update(t)

        except ServiceException as e:
            logging.exception(e)
            return 1

        logging.info('Template updated.')
        return 0
Esempio n. 29
0
  def run_add(self):
    t = Template(self.args.template, user=self.args.username)

    if self.args.username:
      if not self.cs.authenticate(self.args.username, getpass.getpass('Password ({0}): '.format(self.args.username))):
        print('error: unable to authenticate with canvas service.')
        return 1

    # add template bits that are specified
    if self.args.title is not None:
      t.title = self.args.title

    if self.args.description is not None:
      t.description = self.args.description

    if self.args.includes is not None:
      t.includes = self.args.includes

    if self.args.public is not None:
      t.public = self.args.public

    try:
      res = self.cs.template_create(t)

    except ServiceException as e:
      print(e)
      return 1

    print('info: template added.')
    return 0
Esempio n. 30
0
    def run_add(self):
        t = Template(self.args.template, user=self.args.username)

        # add template bits that are specified
        if self.args.title is not None:
            t.title = self.args.title

        if self.args.description is not None:
            t.description = self.args.description

        if self.args.includes is not None:
            t.includes = self.args.includes

        if self.args.public is not None:
            t.public = self.args.public

        try:
            res = self.cs.template_create(t)

        except ServiceException as e:
            logging.exception(e)
            return 1

        logging.info('Template added.')
        return 0
Esempio n. 31
0
  def run_add(self):
    t = Template(self.args.template, user=self.args.username)

    try:
      t = self.cs.template_get(t)

    except ServiceException as e:
      print(e)
      return 1

    # default enabled
    if self.args.enabled is None:
      self.args.enabled = True

    r = Repository(
      stub        = self.args.repo,
      name        = self.args.name,
      baseurl     = self.args.baseurl,
      mirrorlist  = self.args.mirrorlist,
      metalink    = self.args.metalink,
      enabled     = self.args.enabled,
      cost        = self.args.cost,
      priority    = self.args.priority,
      gpgkey      = self.args.gpgkey,
      gpgcheck    = self.args.gpgcheck,
      exclude     = self.args.exclude
    )

    t.add_repo(r)

    # push our updated template
    try:
      res = self.cs.template_update(t)

    except ServiceException as e:
      print(e)
      return 1

    print('info: repo added.')
    return 0
Esempio n. 32
0
    def run_add(self):
        t = Template(self.args.template, user=self.args.username)

        try:
            t = self.cs.template_get(t)

        except ServiceException as e:
            print(e)
            return 1

        # default enabled
        if self.args.enabled is None:
            self.args.enabled = True

        r = Repository({
            'stub': self.args.repo,
            'name': self.args.name,
            'baseurl': self.args.baseurl,
            'mirrorlist': self.args.mirrorlist,
            'metalink': self.args.metalink,
            'enabled': self.args.enabled,
            'cost': self.args.cost,
            'priority': self.args.priority,
            'gpgkey': self.args.gpgkey,
            'gpgcheck': self.args.gpgcheck,
            'exclude': self.args.exclude
        })

        t.add_repo(r)

        # push our updated template
        try:
            res = self.cs.template_update(t)

        except ServiceException as e:
            print(e)
            return 1

        print('info: repo added.')
        return 0
Esempio n. 33
0
    def run_copy(self):
        t = Template(self.args.template_from, user=self.args.username)

        try:
            t = self.cs.template_get(t)

        except ServiceException as e:
            logging.exception(e)
            return 1

        # reparse for template destination
        t.parse(self.args.template_to)

        try:
            res = self.cs.template_create(t)

        except ServiceException as e:
            logging.exception(e)
            return 1

        logging.info('Template copied.')
        return 0
Esempio n. 34
0
    def run_copy(self):
        t = Template(self.args.template_from, user=self.args.username)

        try:
            t = self.cs.template_get(t)

        except ServiceException as e:
            logging.exception(e)
            return 1

        # reparse for template destination
        t.parse(self.args.template_to)

        try:
            res = self.cs.template_create(t)

        except ServiceException as e:
            logging.exception(e)
            return 1

        logging.info('Template copied.')
        return 0
Esempio n. 35
0
    def run_list(self):
        t = Template(self.args.template, user=self.args.username)

        try:
            t = self.cs.template_get(t)

        except ServiceException as e:
            print(e)
            return 1

        packages = list(t.packages_all)
        packages.sort(key=lambda x: x.name)

        if len(packages):
            l = TextTable(header=[
                'PACKAGE', 'EPOCH', 'VERSION', 'RELEASE', 'ARCH', 'ACTION'
            ])

            for p in packages:
                if p.epoch is None:
                    p.epoch = '-'

                if p.version is None:
                    p.version = '-'

                if p.release is None:
                    p.release = '-'

                if p.arch is None:
                    p.arch = '-'

                if p.included:
                    p.action = '+'

                elif p.excluded:
                    p.action = '-'

                elif p.ignored:
                    p.action = '!'

                else:
                    p.action = '?'

                l.add_row(
                    [p.name, p.epoch, p.version, p.release, p.arch, p.action])

            print(l)
            print()

        else:
            print('0 packages defined.')
Esempio n. 36
0
    def run_add(self):
        t = Template(self.args.template, user=self.args.username)

        try:
            t = self.cs.template_get(t)

        except ServiceException as e:
            print(e)
            return 1

        # default enabled
        if self.args.enabled is None:
            self.args.enabled = True

        r = Repository(stub=self.args.repo,
                       name=self.args.name,
                       baseurl=self.args.baseurl,
                       mirrorlist=self.args.mirrorlist,
                       metalink=self.args.metalink,
                       enabled=self.args.enabled,
                       cost=self.args.cost,
                       priority=self.args.priority,
                       gpgkey=self.args.gpgkey,
                       gpgcheck=self.args.gpgcheck,
                       exclude=self.args.exclude)

        t.add_repo(r)

        # push our updated template
        try:
            res = self.cs.template_update(t)

        except ServiceException as e:
            print(e)
            return 1

        print('info: repo added.')
        return 0
Esempio n. 37
0
    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
Esempio n. 38
0
    def _template_resolve_includes(self, template_src):
        if not template_src.includes:
            return template_src

        for i in template_src.includes:
            data = self._template_data_get(Template(i))
            data_resolv = self._template_resolve_includes(data)

            data_resolv._flatten()

            template_src._includes_resolved.append(data_resolv)

        template_src._flatten()
        return template_src
Esempio n. 39
0
    def _template_data_get(self, template):
        if not isinstance(template, Template):
            TypeError('template is not of type Template')

        query = {
            'user': template.user,
            'name': template.name,
            'version': template.version
        }

        query = {k: v for k, v in query.items() if v != None}

        r = urllib.request.Request(
            '%s/api/templates.json?%s' %
            (self._urlbase, urllib.parse.urlencode(query)))

        logging.debug('Fetching template from {0}'.format(r.full_url))

        try:
            u = self._opener.open(r)
            template_summary = json.loads(u.read().decode('utf-8'))

            # nothing returned, so authenticate and retry
            if len(template_summary) == 0 and not self._authenticated:
                self.authenticate()

                u = self._opener.open(r)
                template_summary = json.loads(u.read().decode('utf-8'))

            if len(template_summary):
                # we only have one returned since template names are unique per account
                r = urllib.request.Request('{0}/api/template/{1}.json'.format(
                    self._urlbase, template_summary[0]['uuid']))
                u = self._opener.open(r)
                data = json.loads(u.read().decode('utf-8'))

                return Template(template=data)

            raise ServiceException('unable to get template')

        except urllib.error.URLError as e:
            # TODO: clean up error message
            logging.debug(e)
            raise ServiceException('unknown service response')

        except urllib.error.HTTPError as e:
            # TODO: clean up error message
            logging.debug(e)
            raise ServiceException('unknown service response')
Esempio n. 40
0
  def run_diff(self):
    t = Template(self.args.template_from, user=self.args.username)

    # grab the template we're pushing to
    try:
      t = self.cs.template_get(t)

    except ServiceException as e:
      print(e)
      return 1

    # fetch template to compare to
    if self.args.template_to is not None:
      ts = Template(self.args.template_to, user=self.args.username)

      try:
        ts = self.cs.template_get(ts)

      except ServiceException as e:
        print(e)
        return 1

    # otherwise build from system
    else:
      ts = Template('system')
      ts.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()
Esempio n. 41
0
    def run_list(self):
        t = Template(self.args.template, user=self.args.username)

        try:
            t = self.cs.template_get(t)

        except ServiceException as e:
            print(e)
            return 1

        packages = list(t.packages_all)
        packages.sort(key=lambda x: x.name)

        if len(packages):
            l = prettytable.PrettyTable(
                ['package', 'epoch', 'version', 'release', 'arch', 'action'])
            l.hrules = prettytable.HEADER
            l.vrules = prettytable.NONE
            l.align = 'l'
            l.padding_witdth = 1

            for p in packages:
                if p.epoch is None:
                    p.epoch = '-'

                if p.version is None:
                    p.version = '-'

                if p.release is None:
                    p.release = '-'

                if p.arch is None:
                    p.arch = '-'

                if p.included():
                    p.action = '+'

                else:
                    p.action = '-'

                l.add_row(
                    [p.name, p.epoch, p.version, p.release, p.arch, p.action])

            print(l)
            print()

        else:
            print('0 packages defined.')
Esempio n. 42
0
    def run_list(self):
        t = Template(self.args.template, user=self.args.username)

        try:
            t = self.cs.template_get(t)

        except ServiceException as e:
            print(e)
            return 1

        objects = t.objects
        if objects:
            print('Template {0} has the following objects:'.format(t.name))
            for o in t.objects:
                print('  - ' + str(o))

        else:
            print('Template {0} has no objects.'.format(t.name))
Esempio n. 43
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, 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
Esempio n. 44
0
    def template_get(self, template, auth=False):
        if not isinstance(template, Template):
            TypeError('template is not of type Template')

        # check of force auth
        if auth:
            self.authenticate()

        query = {'user': template.user, 'name': template.name}

        r = urllib.request.Request(
            '%s/api/templates.json?%s' %
            (self._urlbase, urllib.parse.urlencode(query)))

        try:
            u = self._opener.open(r)
            template_summary = json.loads(u.read().decode('utf-8'))

            # nothing returned, so authenticate and retry
            if len(template_summary) == 0 and not self._authenticated:
                self.authenticate()

                u = self._opener.open(r)
                template_summary = json.loads(u.read().decode('utf-8'))

            if len(template_summary):
                # we only have one returned since template names are unique per account
                r = urllib.request.Request('{0}/api/template/{1}.json'.format(
                    self._urlbase, template_summary[0]['uuid']))
                u = self._opener.open(r)
                data = json.loads(u.read().decode('utf-8'))

                return Template(template=data)

            raise ServiceException('unable to get template')

        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:
            print(e)
            raise ServiceException('unknown service response')
Esempio n. 45
0
    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
Esempio n. 46
0
    def run_list(self):
        t = Template(self.args.template, user=self.args.username)

        try:
            t = self.cs.template_get(t)

        except ServiceException as e:
            print(e)
            return 1

        repos = list(t.repos_all)
        repos.sort(key=lambda x: x.stub)

        if len(repos):
            l = prettytable.PrettyTable(
                ['repo', 'name', 'priority', 'cost', 'enabled'])
            l.hrules = prettytable.HEADER
            l.vrules = prettytable.NONE
            l.align = 'l'
            l.padding_witdth = 1

            for r in repos:
                if r.cost is None:
                    r.cost = '-'

                if r.priority is None:
                    r.priority = '-'

                if r.enabled:
                    r.enabled = 'Y'

                else:
                    r.enabled = 'N'

                l.add_row([r.stub, r.name, r.priority, r.cost, r.enabled])

            print(l)
            print()

        else:
            print('0 repos defined.')
Esempio n. 47
0
    def run_pull(self):
        # am i effectively root
        if os.geteuid() != 0:
            logging.error('You need to have root privileges to modify the system.')
            return 0

        t = Template(self.args.template, user=self.args.username)

        try:
            t = self.cs.template_get(t)

        except ServiceException as e:
            logging.exception(e)
            return 1

        t.system_prepare(clean=self.args.pull_clean)

        # 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.')

            logging.info('No action peformed during this dry-run.')
            return 0

        t.system_apply()
Esempio n. 48
0
    def run_dump(self):
        t = Template(self.args.template, user=self.args.username)

        try:
            t = self.cs.template_get(t)

        except ServiceException as e:
            print(e)
            return 1

        if self.args.kickstart:
            print(t.to_kickstart())
            return 0

        elif self.args.yaml:
            print(yaml.dump(t.to_object(), indent=4))
            return 0

        elif self.args.json:
            print(json.dumps(t.to_object(), indent=4))
            return 0

        # pretty general information
        print('Name: {0} ({1})'.format(t.name, t.user))

        if t.description is not None and len(t.description):
            print('Description:\n{0}\n'.format(t.description))

        # pretty print includes
        if len(t.includes):
            print('Includes:')
            for i in t.includes:
                print(' - {0}'.format(i))
            print()

        # pretty print packages
        repos = list(t.repos_all)
        repos.sort(key=lambda x: x.stub)

        if len(repos):
            l = prettytable.PrettyTable(['repo', 'name', 'priority', 'cost', 'enabled'])
            l.min_table_width = 120
            l.hrules = prettytable.HEADER
            l.vrules = prettytable.NONE
            l.align = 'l'
            l.padding_witdth = 1

            for r in repos:
                if r.cost is None:
                    r.cost = '-'

                if r.priority is None:
                    r.priority = '-'

                if r.enabled:
                    r.enabled = 'Y'

                else:
                    r.enabled = 'N'

                l.add_row([r.stub, r.name, r.priority, r.cost, r.enabled])

            print(l)
            print()

        # pretty print packages
        packages = list(t.packages_all)
        packages.sort(key=lambda x: x.name)

        if len(packages):
            l = prettytable.PrettyTable(['package', 'epoch', 'version', 'release', 'arch', 'action'])
            l.min_table_width = 120
            l.hrules = prettytable.HEADER
            l.vrules = prettytable.NONE
            l.align = 'l'
            l.padding_witdth = 1

            for p in packages:
                if p.epoch is None:
                    p.epoch = '-'

                if p.version is None:
                    p.version = '-'

                if p.release is None:
                    p.release = '-'

                if p.arch is None:
                    p.arch = '-'

                if p.included():
                    p.action = '+'

                else:
                    p.action = '-'

                l.add_row([p.name, p.epoch, p.version, p.release, p.arch, p.action])

            print(l)
            print()

        return 0
Esempio n. 49
0
    def run_dump(self):
        t = Template(self.args.template, user=self.args.username)

        try:
            t = self.cs.template_get(t, resolve_includes=not self.args.no_resolve_includes)

        except ServiceException as e:
            logging.exception(e)
            return 1

        if self.args.kickstart:
            print(t.to_kickstart(resolved=not self.args.no_resolve_includes))
            return 0

        elif self.args.yaml:
            print(yaml.dump(t.to_object(resolved=not self.args.no_resolve_includes), indent=4))
            return 0

        elif self.args.json:
            print(json.dumps(t.to_object(resolved=not self.args.no_resolve_includes), indent=4, sort_keys=True))
            return 0

        # pretty general information
        print('TEMPLATE: {0} ({1})\n'.format(t.name, t.user))

        if t.description is not None and len(t.description):
            print('Description:\n{0}\n'.format(t.description))

        # pretty print includes
        if len(t.includes):
            l = TextTable(header=['INCLUDE'])
            for i in t.includes:
                l.add_row([i])

            print(l)
            print()

        # pretty print packages
        repos = list(t.repos_all)
        repos.sort(key=lambda x: x.stub)

        if len(repos):
            l = TextTable(header=["REPO", "NAME", "ENABLED"])

            for r in repos:
                cost = r.cost
                if cost is None:
                    cost = '-'

                priority = r.priority
                if priority is None:
                    priority = '-'

                enabled = 'N'
                if r.enabled:
                    enabled = 'Y'

                #l.add_row([r.stub, r.name, priority, cost, enabled])
                l.add_row([r.stub, r.name, enabled])

            print(l)
            print()

        # pretty print packages
        packages = list(t.packages_all)
        packages.sort(key=lambda x: x.name)

        if len(packages):
            l = TextTable(header=["PACKAGE", "ACTION"])

            for p in packages:
                if p.included:
                    p.action = '+'

                else:
                    p.action = '-'

                l.add_row([p.name, p.action])

            print(l)
            print()

        return 0
Esempio n. 50
0
    def test_template_add_includes_obj(self):
        t1 = Template("foo:bar")
        t2 = Template("bar:baz")
        t3 = Template("baz:daz")

        p1 = Package("foo")
        p2 = Package("bar")
        p3 = Package("baz")
        p4 = Package("~baz")

        t1.add_package(p1)

        t2.add_package(p2)
        t2.add_package(p3)

        t3.add_package(p4)

        # check includes
        t1.includes = [t2]
        self.assertEqual(["bar:baz"], t1.includes)

        # check includes (rebuild)
        t1.includes = [t2, t3]
        self.assertEqual(["bar:baz", "baz:daz"], t1.includes)

        # check package resolution
        t1.includes = [t2]
        self.assertEqual(PackageSet([p1, p2, p3]), t1.packages_all)

        # check package resolution (ordered)
        t1.includes = [t2, t3]
        self.assertEqual(PackageSet([p1, p2, p3]), t1.packages_all)

        t1.includes = [t3, t2]
        self.assertEqual(PackageSet([p1, p2, p4]), t1.packages_all)
Esempio n. 51
0
    def run_iso(self):

        t = Template(self.args.template, user=self.args.username)

        try:
            t = self.cs.template_get(t)

        except ServiceException as e:
            logging.exception(e)
            return 1

        # calculate name and title for use if not specified
        arch = os.uname()[4]

        if arch in ['i386', 'i686']:
            arch_pretty = '32 bit'
            arch_pretty_long = '32 bit (%s)' % (arch)

        elif arch in ['x86_64']:
            arch_pretty = '64 bit'
            arch_pretty_long = '64 bit (%s)' % (arch)

        # we'll use the release version in our names
        if self.args.releasever is None:
            if t.version is None or t.version is '':
                # default to release ver of installed system at /
                self.args.releasever = dnf.rpm.detect_releasever('/')

            else:
                self.args.releasever = t.version

        name              = "{0}".format(t.name)
        name_long         = "{0}-{1}-{2}".format(t.name, self.args.releasever, arch)
        name_pretty       = "{0}-{1}-{2}".format(t.name, self.args.releasever, arch_pretty)
        title             = "{0}".format(t.title)
        title_long        = "{0} - {1} - {2}".format(t.title, self.args.releasever, arch_pretty)
        title_pretty_long = "{0} - {1} - {2}".format(t.title, self.args.releasever, arch_pretty_long)

        # build missing strings
        if self.args.resultdir is None:
            self.args.resultdir = "/var/tmp/canvas/isos/{0}-{1}".format(name_long.lower(), ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(8)))

        if self.args.iso_name is None:
            self.args.iso_name = "{0}.iso".format(name_long.lower())

        if self.args.project is None:
            self.args.project = title

        if self.args.volid is None:
            self.args.volid = name_long.lower()

        if self.args.title is None:
            self.args.title = title_pretty_long

        if self.args.logfile is None:
            self.args.logfile = "/var/tmp/canvas/{0}.log".format(name_long.lower())

        # build kickstart file
        ks_file = "canvas-{0}.ks".format(t.uuid)
        ks_path = os.path.join("/var/tmp/canvas/ks", ks_file)

        try:

            # ensure our resultdir exists
            if not os.path.exists(os.path.dirname(ks_path)):
                os.makedirs(os.path.dirname(ks_path))

            with open(ks_path, 'w') as f:
                f.write(t.to_kickstart(resolved=True))

        except IOError as e:
            logging.error('You need root privileges to build iso at this location.')
            return 1

        env = os.environ.copy()

        # livecd-creator
        if self.args.use_livecd_creator:
            args = [
                    'livecd-creator',
                    '--verbose',
                    '--config',     ks_path,
                    '--fslabel',    name_long.lower(),
                    '--title',      self.args.title,
                    '--releasever', self.args.releasever,
                    '--product',    self.args.project,
                    '--cache',      self.args.resultdir,
                    '--logfile',    self.args.logfile
                ]

            env["setarch"] = arch

        # livemedia-creator
        else:
            args = [
                    'livemedia-creator',
                    '--no-virt',
                    '--make-iso',
                    '--iso-only',
                    '--macboot',
                    '--ks',         ks_path,
                    '--resultdir',  self.args.resultdir,
                    '--project',    self.args.project,
                    '--volid',      self.args.volid,
                    '--iso-name',   self.args.iso_name,
                    '--releasever', self.args.releasever,
                    '--title',      self.args.title,
                    '--logfile',    self.args.logfile
                ]

        subprocess.run(args, env=env)

        return 0
Esempio n. 52
0
    def run_push(self):
        t = Template(self.args.template, user=self.args.username)

        # grab the template we're pushing to
        try:
            t = self.cs.template_get(t)

        except ServiceException as e:
            logging.exception(e)
            return 1

        if self.args.push_clean:
            t.clear()

        if self.args.kickstart is not None:
            logging.info('Parsing kickstart ...')
            t.from_kickstart(self.args.kickstart)

        else:
            # prepare dnf
            logging.info('Analysing system ...')
            db = dnf.Base()
            db.read_all_repos()
            db.read_comps()

            try:
                db.fill_sack()

            except OSError as e:
                pass

            db_list = db.iter_userinstalled()

            if self.args.push_all:
                db_list = db.sack.query().installed()

            # add our user installed packages
            for p in db_list:
                # no need to store versions
                t.add_package(Package(p, evr=False))

            # add only enabled repos
            for r in db.repos.enabled():
                t.add_repo(Repository(r))

        objects = list(t.objects_delta)
        objects.sort(key=lambda x: x.name)

        packages = list(t.packages_delta)
        packages.sort(key=lambda x: x.name)

        repos = list(t.repos_delta)
        repos.sort(key=lambda x: x.name)

        # describe process for dry runs
        if self.args.dry_run:
            if len(packages) or len(repos):
                print('The following would be added to the template: {0}'.format(t.name))

                for r in repos:
                    print('  - ' + str(r))

                for p in packages:
                    print('  - ' + str(p))

                for o in objects:
                    print('  - ' + str(o))

                print()
                print('Summary:')
                print('  - Repo(s): %d' % (len(repos)))
                print('  - Package(s): %d' % (len(packages)))
                print('  - Object(s): %d' % (len(objects)))
                print()

            else:
                print('No template changes required.')

            logging.info('No action peformed during this dry-run.')
            return 0

        if self.args.kickstart is None and not len(packages) and not len(repos):
            logging.info('No changes detected, template up to date.')
            return 0

        # push our updated template
        try:
            res = self.cs.template_update(t)

        except ServiceException as e:
            logging.exception(e)
            return 1

        logging.info('Template pushed.')
        return 0
Esempio n. 53
0
    def run_update(self):
        t = Template(self.args.template, user=self.args.username)

        try:
            t = self.cs.template_get(t)

        except ServiceException as e:
            print(e)
            return 1

        r = t.find_repo(self.args.repo)

        if len(r) != 1:
            print('error: repo is not defined in template.')
            return 1

        r = r[0]

        # reset baseurl, metalink and mirrorlist when any are specified
        if self.args.baseurl is not None or self.args.metalink is not None or self.args.mirrorlist is not None:
            r.baseurl = None
            r.mirrorlist = None
            r.metalink = None

        if self.args.baseurl is not None:
            r.baseurl = self.args.baseurl

        if self.args.cost is not None:
            r.cost = self.args.cost

        if self.args.enabled is not None:
            r.enabled = self.args.enabled

        if self.args.gpgcheck is not None:
            r.gpgcheck = self.args.gpgcheck

        if self.args.gpgkey is not None:
            r.gpgkey = self.args.gpgkey

        if self.args.metalink is not None:
            r.metalink = self.args.metalink

        if self.args.mirrorlist is not None:
            r.mirrorlist = self.args.mirrorlist

        if self.args.name is not None:
            r.name = self.args.name

        if self.args.priority is not None:
            r.priority= self.args.priority

        if not t.update_repo(r):
            print('info: no changes detected.')
            return 0

        # push our updated template
        try:
            res = self.cs.template_update(t)

        except ServiceException as e:
            print(e)
            return 1

        print('info: repo updated.')
        return 0