def _instantiate_artifactory(self): if self.user_creds.is_known_user: # The user has been authenticated try: # Instantiate an Artifactory with the known username and password return Artifactory(*self.user_creds.retrieve_user_creds()) except HTTPError as e: print("Something went wrong! Please check your internet connection and retry." "\nIf the issue persists, please contact the maintainer.") sys.exit(2) else: print("Before performing any action against the server you need to authenticate yourself." "\nPlease provide a valid username and password") user = input("username: "******"password: "******"Username and/or password cannot be empty.\nExiting..") sys.exit(2) try: art = Artifactory(user, password) # The user has been authenticated by the Artifactory server. Store the username and password self.user_creds.store_user_creds(user, password) return art # perform desired action except HTTPError as e: # If the authentication fails (wrong username/ password), no token will be generated if e.response.status_code == 401: print("Error! Please provide valid username and password.\nExiting..") else: print("Something went wrong! Please check your internet connection and retry." "\nIf the issue persists, please contact the maintainer.") sys.exit(2)
def main(): module = AnsibleModule(argument_spec=dict( artifactory_url=dict(required=False, type="str"), artifactory_username=dict(required=False, type="str"), artifactory_password=dict(required=False, type="str"), artifactory_redirect=dict(required=False, type="str", default="artifactory"), action=dict(required=True, type="str"), api_key=dict(required=False, type="str", default=""), user=dict(required=False, type="str", default=""), ), supports_check_mode=True) if not HAS_ARTIFACTORY: module.fail_json(msg=INSTALL_ARTIFACTORY_MSG) action = module.params.get("action") artifactory_url = os.environ.get("ARTIFACTORY_URL", module.params.get("artifactory_url")) artifactory_username = os.environ.get( "ARTIFACTORY_USERNAME", module.params.get("artifactory_username")) artifactory_password = os.environ.get( "ARTIFACTORY_PASSWORD", module.params.get("artifactory_password")) artifactory_redirect = os.environ.get( "ARTIFACTORY_REDIRECT", module.params.get("artifactory_redirect")) # create a artifactory client instance artifactory = Artifactory( url=artifactory_url, username=artifactory_username, password=artifactory_password, redirect=artifactory_redirect, ) try: if action.lower() == "get": apikey = artifactory.security.apikeys.fetch() module.exit_json(changed=True, msg="") elif action.lower() == "create": apikey = artifactory.security.apikeys.new() apikey.api_key = module.params.get("api_key") apikey = apikey.create() module.exit_json(changed=True, msg=apikey.api_key) elif action.lower() == "revoke": response = artifactory.security.apikeys.revoke( module.params.get("user")) module.exit_json(changed=True, msg=response) except Exception, e: module.fail_json(msg=e)
def main(): module = AnsibleModule( argument_spec=dict( # create ldap params name=dict(required=False, type="str"), url=dict(required=False, type="str"), search_filter=dict(required=False, type="str"), search_base=dict(required=False, type="str", default=""), manager_dn=dict(required=False, type="str"), manager_password=dict(required=False, type="str"), email_attribute=dict(required=False, type="str"), group_name=dict(required=False, type="str"), group_search_base=dict(required=False, type="str"), group_name_attribute=dict(required=False, type="str"), group_member_attribute=dict(required=False, type="str"), group_filter=dict(required=False, type="str"), description_attribute=dict(required=False, type="str"), # import dl params ldap_group_name=dict(required=False, type="str"), ldap_group_list=dict(required=False, type="list", default=[]), action=dict(required=True, type="str"), artifactory_url=dict(required=False, type="str"), artifactory_username=dict(required=False, type="str"), artifactory_password=dict(required=False, type="str"), artifactory_redirect=dict(required=False, type="str", default="artifactory"), ), supports_check_mode=True) if not HAS_ARTIFACTORY: module.fail_json(msg=INSTALL_ARTIFACTORY_MSG) action = module.params.get("action") artifactory_url = os.environ.get("ARTIFACTORY_URL", module.params.get("artifactory_url")) artifactory_username = os.environ.get( "ARTIFACTORY_USERNAME", module.params.get("artifactory_username")) artifactory_password = os.environ.get( "ARTIFACTORY_PASSWORD", module.params.get("artifactory_password")) artifactory_redirect = os.environ.get( "ARTIFACTORY_REDIRECT", module.params.get("artifactory_redirect")) # create a artifactory client instance artifactory = Artifactory( url=artifactory_url, username=artifactory_username, password=artifactory_password, redirect=artifactory_redirect, ) try: if action.lower() == "create": ldap = artifactory.system.configuration.ldap() # ldap settings ldap.name = module.params.get("name") ldap.url = module.params.get("url") ldap.search_filter = module.params.get("search_filter") ldap.manager_dn = module.params.get("manager_dn") ldap.manager_password = module.params.get("manager_password") ldap.email_attribute = module.params.get("email_attribute") ldap.group_name = module.params.get("group_name") ldap.group_search_base = module.params.get("group_search_base") ldap.group_name_attribute = module.params.get( "group_name_attribute") ldap.group_member_attribute = module.params.get( "group_member_attribute") ldap.group_filter = module.params.get("group_filter") ldap.description_attribute = module.params.get( "description_attribute") response = ldap.update() module.exit_json(changed=True, msg=response) elif action.lower() == "import_group": changed = False response = "Groups already imported" artifactory_groups = [ group.name for group in artifactory.security.groups.list() ] for dl in module.params.get("ldap_group_list"): if dl not in artifactory_groups: changed = True if changed: ldap_groups = artifactory.ldap_groups.new() ldap_groups.group_name = module.params.get("ldap_group_name") ldap_groups.group_list = module.params.get("ldap_group_list") response = ldap_groups.import_group() module.exit_json(changed=changed, msg=response) except Exception, e: module.fail_json(msg=e)
def main(): module = AnsibleModule(argument_spec=dict( artifactory_url=dict(required=False, type="str"), artifactory_username=dict(required=False, type="str"), artifactory_password=dict(required=False, type="str"), artifactory_redirect=dict(required=False, type="str", default="artifactory"), action=dict(required=True, type="str"), name=dict(required=True, type="str"), description=dict(required=False, type="str"), auto_join=dict(required=False, type="bool", default=False), realm=dict(required=False, type="str"), realm_attributes=dict(required=False, type="str"), ), supports_check_mode=True) if not HAS_ARTIFACTORY: module.fail_json(msg=INSTALL_ARTIFACTORY_MSG) action = module.params.get("action") artifactory_url = os.environ.get("ARTIFACTORY_URL", module.params.get("artifactory_url")) artifactory_username = os.environ.get( "ARTIFACTORY_USERNAME", module.params.get("artifactory_username")) artifactory_password = os.environ.get( "ARTIFACTORY_PASSWORD", module.params.get("artifactory_password")) artifactory_redirect = os.environ.get( "ARTIFACTORY_REDIRECT", module.params.get("artifactory_redirect")) # create a artifactory client instance artifactory = Artifactory( url=artifactory_url, username=artifactory_username, password=artifactory_password, redirect=artifactory_redirect, ) try: if action.lower() == "create": group = artifactory.security.groups.new() group.name = module.params.get("name") group.description = module.params.get("description") group.auto_join = module.params.get("auto_join") group.realm = module.params.get("realm") group.realm_attributes = module.params.get("realm_attributes") response = group.create() module.exit_json(changed=True, msg=response) elif action.lower() == "update": params = module.params group = artifactory.security.groups.fetch(params.get("name")) if params.get("name"): group.name = params.get("name") if params.get("description"): group.description = params.get("description") if params.get("auto_join"): group.auto_join = params.get("auto_join") if params.get("realm"): group.realm = params.get("realm") if params.get("realm_attributes"): group.realm_attributes = params.get("realm_attributes") else: group.realm_attributes = "" response = group.update() module.exit_json(changed=True, msg=response) elif action.lower() == "delete": group = artifactory.security.groups.fetch( module.params.get("name")) response = group.remove() module.exit_json(changed=True, msg=response) except Exception, e: module.fail_json(msg=e)
def main(): argument_spec = ArgumentSpecs.all() argument_spec.update( artifactory_url=dict(required=False, type="str"), artifactory_username=dict(required=False, type="str"), artifactory_password=dict(required=False, type="str"), artifactory_redirect=dict(required=False, type="str", default="artifactory"), action=dict(required=True, type="str"), type=dict(required=False, type="str"), path=dict(required=False, type="str"), suffix=dict(required=False, type="str", default=""), ) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) if not HAS_ARTIFACTORY: module.fail_json(msg=INSTALL_ARTIFACTORY_MSG) # get module params action = module.params.get("action") type = module.params.get("type") suffix = module.params.get("suffix") artifactory_url = os.environ.get("ARTIFACTORY_URL", module.params.get("artifactory_url")) artifactory_username = os.environ.get( "ARTIFACTORY_USERNAME", module.params.get("artifactory_username")) artifactory_password = os.environ.get( "ARTIFACTORY_PASSWORD", module.params.get("artifactory_password")) artifactory_redirect = os.environ.get( "ARTIFACTORY_REDIRECT", module.params.get("artifactory_redirect")) # yml file path for bulk create/update/delete path = module.params.get("path") if path: path = os.path.abspath(path) # append suffix to repository name if suffix: module.params["key"] = "{0}{1}".format(module.params.get("key"), suffix) # create a artifactory client instance artifactory = Artifactory( url=artifactory_url, username=artifactory_username, password=artifactory_password, redirect=artifactory_redirect, ) if action.lower() == "create": try: artifactory.repository.fetch(module.params.get("key")) module.exit_json(changed=False, msg="Repository {0} already exists".format( module.params.get("key"))) except Exception: if type.lower() == "local": response = create_local(artifactory, module.params) module.exit_json(changed=True, msg=response) elif type.lower() == "remote": response = create_remote(artifactory, module.params) module.exit_json(changed=True, msg=response) elif type.lower() == "virtual": response = create_virtual(artifactory, module.params) module.exit_json(changed=True, msg=response) else: module.fail_json( msg="Repository type {0} not found.".format(type)) except Exception, e: module.fail_json(msg=e)
def main(): module = AnsibleModule(argument_spec=dict( artifactory_url=dict(required=False, type="str"), artifactory_username=dict(required=False, type="str"), artifactory_password=dict(required=False, type="str"), artifactory_redirect=dict(required=False, type="str", default="artifactory"), action=dict(required=True, type="str"), name=dict(required=True, type="str"), email=dict(required=False, type="str"), password=dict(required=False, type="str"), groups=dict(required=False, type="list", default=[]), admin=dict(required=False, type="bool", default=False), profile_updatable=dict(required=False, type="bool", default=True), internal_password_disabled=dict(required=False, type="bool", default=False), ), supports_check_mode=True) if not HAS_ARTIFACTORY: module.fail_json(msg=INSTALL_ARTIFACTORY_MSG) action = module.params.get("action") artifactory_url = os.environ.get("ARTIFACTORY_URL", module.params.get("artifactory_url")) artifactory_username = os.environ.get( "ARTIFACTORY_USERNAME", module.params.get("artifactory_username")) artifactory_password = os.environ.get( "ARTIFACTORY_PASSWORD", module.params.get("artifactory_password")) artifactory_redirect = os.environ.get( "ARTIFACTORY_REDIRECT", module.params.get("artifactory_redirect")) # create a artifactory client instance artifactory = Artifactory( url=artifactory_url, username=artifactory_username, password=artifactory_password, redirect=artifactory_redirect, ) try: if action.lower() == "create": user = artifactory.security.users.new() user.name = module.params.get("name") user.password = module.params.get("password") user.email = module.params.get("email") user.groups = module.params.get("groups") user.admin = module.params.get("admin") user.profile_updatable = module.params.get("profile_updatable") user.internal_password_disabled = module.params.get( "internal_password_disabled") response = user.create() module.exit_json(changed=True, msg=response) elif action.lower() == "update": params = module.params user = artifactory.security.users.fetch(params.get("name")) if params.get("name"): user.name = params.get("name") if params.get("password"): user.password = params.get("password") else: user.password = "" if params.get("email"): user.email = params.get("email") if params.get("groups"): user.groups = params.get("groups") if params.get("admin"): user.admin = params.get("admin") if params.get("profile_updatable"): user.profile_updatable = params.get("profile_updatable") if params.get("internal_password_disabled"): user.internal_password_disabled = params.get( "internal_password_disabled") else: user.internal_password_disabled = False response = user.update() module.exit_json(changed=True, msg=response) elif action.lower() == "delete": user = artifactory.security.users.fetch(module.params.get("name")) response = user.remove() module.exit_json(changed=True, msg=response) except Exception, e: module.fail_json(msg=e)
def main(): module = AnsibleModule(argument_spec=dict( artifactory_url=dict(required=False, type="str"), artifactory_username=dict(required=False, type="str"), artifactory_password=dict(required=False, type="str"), artifactory_redirect=dict(required=False, type="str", default="artifactory"), path=dict(required=False, type="str"), action=dict(required=True, type="str"), name=dict(required=False, type="str"), repositories=dict(required=False, type="list"), includes_pattern=dict(required=False, type="str", default="**"), excludes_pattern=dict(required=False, type="str", default=""), permissions=dict(required=False, type="dict", default={}), suffix=dict(required=False, type="str", default=""), ), supports_check_mode=True) if not HAS_ARTIFACTORY: module.fail_json(msg=INSTALL_ARTIFACTORY_MSG) action = module.params.get("action") suffix = module.params.get("suffix") artifactory_url = os.environ.get("ARTIFACTORY_URL", module.params.get("artifactory_url")) artifactory_username = os.environ.get( "ARTIFACTORY_USERNAME", module.params.get("artifactory_username")) artifactory_password = os.environ.get( "ARTIFACTORY_PASSWORD", module.params.get("artifactory_password")) artifactory_redirect = os.environ.get( "ARTIFACTORY_REDIRECT", module.params.get("artifactory_redirect")) path = module.params.get("path") if path: path = os.path.abspath(path) # create a artifactory client instance artifactory = Artifactory( url=artifactory_url, username=artifactory_username, password=artifactory_password, redirect=artifactory_redirect, ) try: if action.lower() == "create": changed = False try: artifactory.security.permissions.fetch( module.params.get("name")) response = "Permission {0} already exists".format( module.params.get("name")) except: response = create_permission(artifactory, module.params) changed = True module.exit_json(changed=changed, msg=response) elif action.lower() == "update": response = update_permission(artifactory, module.params) module.exit_json(changed=True, msg=response) elif action.lower() == "delete": changed = False try: permission = artifactory.security.permissions.fetch( module.params.get("name")) response = permission.remove() changed = True except: response = "Permission {0} does not exists".format( module.params.get("name")) module.exit_json(changed=changed, msg=response) elif action.lower() == "bulk_create": changed = False response = [] params = yaml.load(open(path, 'r')) for project_name, services in params.get("projects").iteritems(): if services.get("artifactory").get("permissions"): for permission_params in services.get("artifactory").get( "permissions"): try: artifactory.security.permissions.fetch( permission_params.get("name")) response.append( "Permission {0} already exists".format( permission_params.get("name"))) except: response.append( create_permission(artifactory, permission_params)) changed = True module.exit_json(changed=changed, msg=response) elif action.lower() == "bulk_update": response = [] params = yaml.load(open(path, 'r')) for project_name, services in params.get("projects").iteritems(): if services.get("artifactory").get("permissions"): for permission_params in services.get("artifactory").get( "permissions"): response.append( update_permission(artifactory, permission_params)) module.exit_json(changed=True, msg=response) elif action.lower() == "bulk_delete": changed = False response = [] params = yaml.load(open(path, 'r')) for project_name, services in params.get("projects").iteritems(): if services.get("artifactory").get("permissions"): for permission_params in services.get("artifactory").get( "permissions"): try: permission = artifactory.security.permissions.fetch( permission_params.get("name")) response.append(permission.remove()) changed = True except Exception: response.append( "Permission {0} does not exists".format( permission_params.get("name"))) module.exit_json(changed=changed, msg=response) except Exception, e: module.fail_json(msg=e)
def artifactory(): return Artifactory( "http://127.0.0.1:8081", username="******", password="******", )