Debian Codegraph @

ansible_collections/kubernetes/core/plugins/modules/k8s_cp.py

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 # Copyright: (c) 2021, Aubin Bikouo <@abikouo>
4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6 from __future__ import absolute_import, division, print_function
7
8 __metaclass__ = type
9
10
11 DOCUMENTATION = r"""
12
13 module: k8s_cp
14
15 short_description: Copy files and directories to and from pod.
16
17 version_added: 2.2.0
18
19 author:
20 - Aubin Bikouo (@abikouo)
21
22 description:
23 - Use the Kubernetes Python client to copy files and directories to and from containers inside a pod.
24
25 extends_documentation_fragment:
26 - kubernetes.core.k8s_auth_options
27
28 requirements:
29 - "python >= 3.9"
30 - "kubernetes >= 24.2.0"
31
32 options:
33 namespace:
34 description:
35 - The pod namespace name.
36 type: str
37 required: yes
38 pod:
39 description:
40 - The pod name.
41 type: str
42 required: yes
43 container:
44 description:
45 - The name of the container in the pod to copy files/directories from/to.
46 - Defaults to the only container if there is only one container in the pod.
47 type: str
48 remote_path:
49 description:
50 - Path of the file or directory to copy.
51 type: path
52 required: yes
53 local_path:
54 description:
55 - Path of the local file or directory.
56 - Required when I(state) is set to C(from_pod).
57 - Mutually exclusive with I(content).
58 type: path
59 content:
60 description:
61 - When used instead of I(local_path), sets the contents of a local file directly to the specified value.
62 - Works only when I(remote_path) is a file. Creates the file if it does not exist.
63 - For advanced formatting or if the content contains a variable, use the M(ansible.builtin.template) module.
64 - Mutually exclusive with I(local_path).
65 type: str
66 state:
67 description:
68 - When set to C(to_pod), the local I(local_path) file or directory will be copied to I(remote_path) into the pod.
69 - When set to C(from_pod), the remote file or directory I(remote_path) from pod will be copied locally to I(local_path).
70 type: str
71 default: to_pod
72 choices: [ to_pod, from_pod ]
73 no_preserve:
74 description:
75 - The copied file/directory's ownership and permissions will not be preserved in the container.
76 - This option is ignored when I(content) is set or when I(state) is set to C(from_pod).
77 type: bool
78 default: False
79
80 notes:
81 - the tar binary is required on the container when copying from local filesystem to pod.
82 - the (init) container has to be started before you copy files or directories to it.
83 """
84
85 EXAMPLES = r"""
86 # kubectl cp /tmp/foo some-namespace/some-pod:/tmp/bar
87 - name: Copy /tmp/foo local file to /tmp/bar in a remote pod
88 kubernetes.core.k8s_cp:
89 namespace: some-namespace
90 pod: some-pod
91 remote_path: /tmp/bar
92 local_path: /tmp/foo
93
94 # kubectl cp /tmp/foo_dir some-namespace/some-pod:/tmp/bar_dir
95 - name: Copy /tmp/foo_dir local directory to /tmp/bar_dir in a remote pod
96 kubernetes.core.k8s_cp:
97 namespace: some-namespace
98 pod: some-pod
99 remote_path: /tmp/bar_dir
100 local_path: /tmp/foo_dir
101
102 # kubectl cp /tmp/foo some-namespace/some-pod:/tmp/bar -c some-container
103 - name: Copy /tmp/foo local file to /tmp/bar in a remote pod in a specific container
104 kubernetes.core.k8s_cp:
105 namespace: some-namespace
106 pod: some-pod
107 container: some-container
108 remote_path: /tmp/bar
109 local_path: /tmp/foo
110 no_preserve: True
111 state: to_pod
112
113 # kubectl cp some-namespace/some-pod:/tmp/foo /tmp/bar
114 - name: Copy /tmp/foo from a remote pod to /tmp/bar locally
115 kubernetes.core.k8s_cp:
116 namespace: some-namespace
117 pod: some-pod
118 remote_path: /tmp/foo
119 local_path: /tmp/bar
120 state: from_pod
121
122 # copy content into a file in the remote pod
123 - name: Copy content into a file in the remote pod
124 kubernetes.core.k8s_cp:
125 state: to_pod
126 namespace: some-namespace
127 pod: some-pod
128 remote_path: /tmp/foo.txt
129 content: "This content will be copied into remote file"
130 """
131
132
133 RETURN = r"""
134 result:
135 description:
136 - message describing the copy operation successfully done.
137 returned: success
138 type: str
139 """
140
141 import copy
142
143 from ansible.module_utils.common.text.converters import to_native
144 from ansible_collections.kubernetes.core.plugins.module_utils.ansiblemodule import (
145 AnsibleModule,
146 )
147 from ansible_collections.kubernetes.core.plugins.module_utils.args_common import (
148 AUTH_ARG_SPEC,
149 )
150 from ansible_collections.kubernetes.core.plugins.module_utils.copy import (
151 K8SCopyFromPod,
152 K8SCopyToPod,
153 check_pod,
154 )
155 from ansible_collections.kubernetes.core.plugins.module_utils.k8s.client import (
156 get_api_client,
157 )
158 from ansible_collections.kubernetes.core.plugins.module_utils.k8s.core import (
159 AnsibleK8SModule,
160 )
161 from ansible_collections.kubernetes.core.plugins.module_utils.k8s.exceptions import (
162 CoreException,
163 )
164 from ansible_collections.kubernetes.core.plugins.module_utils.k8s.service import (
165 K8sService,
166 )
167
168
169 def argspec():
170 argument_spec = copy.deepcopy(AUTH_ARG_SPEC)
171 argument_spec["namespace"] = {"type": "str", "required": True}
172 argument_spec["pod"] = {"type": "str", "required": True}
173 argument_spec["container"] = {}
174 argument_spec["remote_path"] = {"type": "path", "required": True}
175 argument_spec["local_path"] = {"type": "path"}
176 argument_spec["content"] = {"type": "str"}
177 argument_spec["state"] = {
178 "type": "str",
179 "default": "to_pod",
180 "choices": ["to_pod", "from_pod"],
181 }
182 argument_spec["no_preserve"] = {"type": "bool", "default": False}
183 return argument_spec
184
185
186 def execute_module(module):
187 client = get_api_client(module=module)
188 svc = K8sService(client, module)
189 containers = check_pod(svc)
190 if len(containers) > 1 and module.params.get("container") is None:
191 module.fail_json(
192 msg="Pod contains more than 1 container, option 'container' should be set"
193 )
194
195 state = module.params.get("state")
196 if state == "to_pod":
197 k8s_copy = K8SCopyToPod(module, client.client)
198 else:
199 k8s_copy = K8SCopyFromPod(module, client.client)
200
201 try:
202 k8s_copy.run()
203 except Exception as e:
204 module.fail_json("Failed to copy object due to: {0}".format(to_native(e)))
205
206
207 def main():
208 module = AnsibleK8SModule(
209 module_class=AnsibleModule,
210 argument_spec=argspec(),
211 check_pyyaml=False,
212 mutually_exclusive=[("local_path", "content")],
213 required_if=[("state", "from_pod", ["local_path"])],
214 required_one_of=[["local_path", "content"]],
215 supports_check_mode=True,
216 )
217
218 try:
219 execute_module(module)
220 except CoreException as e:
221 module.fail_from_exception(e)
222
223
224 if __name__ == "__main__":
225 main()