diff --git a/cloudify_docker/tasks.py b/cloudify_docker/tasks.py index 3582ff1..2f27b3e 100644 --- a/cloudify_docker/tasks.py +++ b/cloudify_docker/tasks.py @@ -571,8 +571,17 @@ def list_images(ctx, docker_client, **kwargs): @operation -@handle_docker_exception def install_docker(ctx, **kwargs): + resource_config = ctx.node.properties.get('resource_config', {}) + offline_installation = resource_config.get('offline_installation') + if not offline_installation: + _install_docker(ctx=ctx, **kwargs) + else: + _install_docker_offline(ctx=ctx, **kwargs) + + +@handle_docker_exception +def _install_docker(ctx, **kwargs): # fetch the data needed for installation docker_ip, docker_user, docker_key, _ = get_docker_machine_from_ctx(ctx) resource_config = ctx.node.properties.get('resource_config', {}) @@ -601,8 +610,76 @@ def install_docker(ctx, **kwargs): call_command(_command, fab_ctx=s) +@handle_docker_exception +def _install_docker_offline(ctx, **kwargs): + """ + support only for EDGE OS (ubuntu22.04) + """ + # fetch the data needed for installation + docker_ip, docker_user, docker_key, _ = get_docker_machine_from_ctx(ctx) + resource_config = ctx.node.properties.get('resource_config', {}) + package_tar_path = resource_config.get('package_tar_path') + post_install_path = resource_config.get('post_install_script_path') + installation_dir = resource_config.get('installation_dir') + install_with_sudo = resource_config.get('install_with_sudo', True) + installation_dir = installation_dir if installation_dir.endswith('/')\ + else '{0}/'.format(installation_dir) + if not (package_tar_path and post_install_path): + raise NonRecoverableError("Please validate your install config") + installation_commands = [ + 'tar -xf {0} -C {1}'.format(package_tar_path, installation_dir), + 'dpkg -i {0}*.deb'.format(installation_dir), + 'chmod 0755 {0}'.format(post_install_path), + 'sh {}'.format(post_install_path), + 'usermod -aG docker {0}'.format(docker_user) + ] + + with get_fabric_settings(ctx, docker_ip, docker_user, docker_key) as s: + with s: + for _command in installation_commands: + if install_with_sudo: + call_sudo(_command, fab_ctx=s) + else: + call_command(_command, fab_ctx=s) + + @operation def uninstall_docker(ctx, **kwargs): + resource_config = ctx.node.properties.get('resource_config', {}) + offline_installation = resource_config.get('offline_installation') + if not offline_installation: + _uninstall_docker(ctx=ctx, **kwargs) + else: + _uninstall_docker_offline(ctx=ctx, **kwargs) + + +def _uninstall_docker_offline(ctx, **kwargs): + """ + support only for EDGE OS (ubuntu22.04) + """ + # fetch the data needed for installation + docker_ip, docker_user, docker_key, _ = get_docker_machine_from_ctx(ctx) + resource_config = ctx.node.properties.get('resource_config', {}) + install_with_sudo = resource_config.get('install_with_sudo', True) + installation_dir = resource_config.get('installation_dir') + installation_dir = installation_dir if installation_dir.endswith('/') \ + else '{0}/'.format(installation_dir) + installation_commands = [ + 'dpkg --remove docker-buildx-plugin docker-ce docker-ce-rootless-' + 'extras docker-ce-cli docker-compose-plugin containerd.io', + 'rm -rf {0}'.format(installation_dir) + ] + + with get_fabric_settings(ctx, docker_ip, docker_user, docker_key) as s: + with s: + for _command in installation_commands: + if install_with_sudo: + call_sudo(_command, fab_ctx=s) + else: + call_command(_command, fab_ctx=s) + + +def _uninstall_docker(ctx, **kwargs): # fetch the data needed for installation docker_ip, docker_user, docker_key, _ = get_docker_machine_from_ctx(ctx) resource_config = ctx.node.properties.get('resource_config', {}) @@ -667,28 +744,6 @@ def list_containers(ctx, docker_client, **kwargs): docker_client.containers.list(all=True, trunc=True) -@operation -@handle_docker_exception -@with_docker -def pull_image(ctx, docker_client, **kwargs): - resource_config = ctx.node.properties.get('resource_config', {}) - tag = resource_config.get('tag') - all_tags = resource_config.get('all_tags', False) - if not tag: - return - repository = tag.split(':')[0] - try: - image_tag = tag.split(':')[1] - except IndexError: - image_tag = 'latest' - try: - docker_client.images.get(tag) - except ImageNotFound: - docker_client.images.pull(repository=repository, - tag=image_tag, all_tags=all_tags) - ctx.instance.runtime_properties['build_result'] = 'Image was pull' - - @operation @handle_docker_exception @with_docker @@ -697,6 +752,8 @@ def build_image(ctx, docker_client, **kwargs): image_content, tag = get_from_resource_config(resource_config, 'image_content', 'tag') + pull_image = resource_config.get('pull_image', False) + if image_content: # check what content we got, URL , path or string split = image_content.split('://') @@ -729,6 +786,21 @@ def build_image(ctx, docker_client, **kwargs): raise NonRecoverableError("Build Failed check build-result") ctx.instance.runtime_properties['image'] = \ repr(docker_client.images.get(name=tag)) + elif pull_image: + all_tags = resource_config.get('all_tags', False) + if not tag: + return + repository = tag.split(':')[0] + try: + image_tag = tag.split(':')[1] + except IndexError: + image_tag = 'latest' + try: + docker_client.images.get(tag) + except ImageNotFound: + docker_client.images.pull(repository=repository, + tag=image_tag, all_tags=all_tags) + ctx.instance.runtime_properties['build_result'] = 'Image was pull' @operation @@ -1120,3 +1192,4 @@ def remove_container(ctx, docker_client, **kwargs): remove_res = container_obj.remove() ctx.instance.runtime_properties.pop('container') ctx.logger.info("Remove result {0}".format(remove_res)) + diff --git a/plugin.yaml b/plugin.yaml index 5899993..cd5bee8 100644 --- a/plugin.yaml +++ b/plugin.yaml @@ -173,6 +173,28 @@ data_types: type: boolean description: use sudo to run default: true + offline_installation: + type: boolean + description: Install docker when the vm has no internet access + default: false + package_tar_path: + description: | + Docker Installation Tar path (must be located on the on where docker installed) + Required when offline installation + type: string + default: '' + post_install_script_path: + description: | + Docker Installation post script path + Required when offline installation + type: string + default: '' + installation_dir: + description: | + Docker Installation path + Required when offline installation + type: string + default: '' cloudify.types.docker.ClientConfig: properties: @@ -201,16 +223,13 @@ data_types: description: Docker image tag type: string default: '' - - cloudify.types.docker.PullImage: - properties: - tag: - description: Docker image tag - type: string - default: '' + pull_image: + type: boolean + description: Pull image + default: false all_tags: type: boolean - description: Pull all tags + description: Pull all tags (only if pull_image is True) default: false cloudify.types.docker.Container: @@ -323,7 +342,6 @@ node_types: delete: implementation: docker.cloudify_docker.tasks.uninstall_docker - cloudify.nodes.docker.host_details: derived_from: cloudify.nodes.Root properties: @@ -348,21 +366,6 @@ node_types: delete: implementation: docker.cloudify_docker.tasks.remove_image - cloudify.nodes.docker.pullimage: - derived_from: cloudify.nodes.Root - properties: - <<: *client_config - resource_config: - type: cloudify.types.docker.PullImage - description: Docker Image type - required: true - interfaces: - cloudify.interfaces.lifecycle: - create: - implementation: docker.cloudify_docker.tasks.pull_image - delete: - implementation: docker.cloudify_docker.tasks.remove_image - cloudify.nodes.docker.container: derived_from: cloudify.nodes.Root properties: diff --git a/plugin_1_4.yaml b/plugin_1_4.yaml index 99a79fa..55b9b94 100644 --- a/plugin_1_4.yaml +++ b/plugin_1_4.yaml @@ -171,6 +171,28 @@ data_types: type: boolean description: use sudo to run default: true + offline_installation: + type: boolean + description: Install docker when the vm has no internet access + default: false + package_tar_path: + description: | + Docker Installation Tar path (must be located on the on where docker installed) + Required when offline installation + type: string + default: '' + post_install_script_path: + description: | + Docker Installation post script path + Required when offline installation + type: string + default: '' + installation_dir: + description: | + Docker Installation path + Required when offline installation + type: string + default: '' cloudify.types.docker.ClientConfig: properties: @@ -199,6 +221,14 @@ data_types: description: Docker image tag type: string default: '' + pull_image: + type: boolean + description: Pull image + default: false + all_tags: + type: boolean + description: Pull all tags (only if pull_image is True) + default: false cloudify.types.docker.PullImage: properties: diff --git a/v2_plugin.yaml b/v2_plugin.yaml index 99a79fa..55b9b94 100644 --- a/v2_plugin.yaml +++ b/v2_plugin.yaml @@ -171,6 +171,28 @@ data_types: type: boolean description: use sudo to run default: true + offline_installation: + type: boolean + description: Install docker when the vm has no internet access + default: false + package_tar_path: + description: | + Docker Installation Tar path (must be located on the on where docker installed) + Required when offline installation + type: string + default: '' + post_install_script_path: + description: | + Docker Installation post script path + Required when offline installation + type: string + default: '' + installation_dir: + description: | + Docker Installation path + Required when offline installation + type: string + default: '' cloudify.types.docker.ClientConfig: properties: @@ -199,6 +221,14 @@ data_types: description: Docker image tag type: string default: '' + pull_image: + type: boolean + description: Pull image + default: false + all_tags: + type: boolean + description: Pull all tags (only if pull_image is True) + default: false cloudify.types.docker.PullImage: properties: