-
Notifications
You must be signed in to change notification settings - Fork 110
/
shutit_setup.py
201 lines (150 loc) · 7.27 KB
/
shutit_setup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
"""
shutit.tk.setup (core ShutIt setup module)
Nomenclature:
- Host machine: Machine on which this script is run.
- Target: Environment to which we deploy (docker container or bash shell)
- Container: Docker container created to run the modules on.
- target_child pexpect-spawned child created to build on target
- host_child pexpect spawned child living on the host machine
"""
# The MIT License (MIT)
#
# Copyright (C) 2014 OpenBet Limited
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
# of the Software, and to permit persons to whom the Software is furnished to do
# so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# ITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
from __future__ import print_function
import shutit_global
import shutit_util
from shutit_module import ShutItModule
from shutit_sendspec import ShutItSendSpec
from shutit_pexpect_session import ShutItPexpectSession
class ShutItConnModule(ShutItModule):
def __init__(self, *args, **kwargs):
super(ShutItConnModule, self).__init__(*args, **kwargs)
def setup_host_child(self, shutit):
shutit.setup_host_child_environment()
def setup_target_child(self, shutit, target_child, target_child_id='target_child',prefix='root'):
shutit.setup_target_child_environment(target_child, target_child_id=target_child_id,prefix=prefix)
def build(self, shutit):
return True
class ConnDocker(ShutItConnModule):
"""Connects ShutIt to docker daemon and starts the container.
"""
def is_installed(self, shutit):
"""Always considered false for ShutIt setup.
"""
return False
def destroy_container(self, shutit, host_shutit_session_name, container_shutit_session_name, container_id):
host_child = shutit.get_shutit_pexpect_session_from_id(host_shutit_session_name).pexpect_child
shutit.conn_docker_destroy_container(host_shutit_session_name, container_shutit_session_name, container_id)
shutit.send(' command docker rm -f ' + container_id + ' && rm -f ' + shutit.build['cidfile'],shutit_pexpect_child=host_child,expect=shutit.expect_prompts['ORIGIN_ENV'])
def start_container(self, shutit, shutit_session_name):
return shutit.conn_docker_start_container(shutit_session_name)
def build(self, shutit):
"""Sets up the target ready for building.
"""
target_child = self.start_container(shutit, 'target_child')
self.setup_host_child(shutit)
# TODO: on the host child, check that the image running has bash as its cmd/entrypoint.
self.setup_target_child(shutit, target_child)
shutit.send('chmod -R 777 ' + shutit_global.shutit_global_object.shutit_state_dir + ' && mkdir -p ' + shutit_global.shutit_global_object.shutit_state_dir_build_db_dir + '/' + shutit_global.shutit_global_object.build_id, shutit_pexpect_child=target_child, echo=False)
return True
def finalize(self, shutit):
"""Finalizes the target, exiting for us back to the original shell
and performing any repository work required.
"""
# Finish with the target
target_child_pexpect_session = shutit.get_shutit_pexpect_session_from_id('target_child')
assert not target_child_pexpect_session.sendline(ShutItSendSpec(target_child_pexpect_session,'exit',ignore_background=True)), shutit_util.print_debug()
host_child_pexpect_session = shutit.get_shutit_pexpect_session_from_id('host_child')
host_child = host_child_pexpect_session.pexpect_child
shutit.set_default_shutit_pexpect_session(host_child_pexpect_session)
shutit.set_default_shutit_pexpect_session_expect(shutit.expect_prompts['ORIGIN_ENV'])
shutit.do_repository_work(shutit.repository['name'], docker_executable=shutit.host['docker_executable'], password=shutit.host['password'])
# Final exits
host_child.sendline('rm -f ' + shutit.build['cidfile']) # Ignore response, just send.
host_child.sendline('exit') # Exit raw bash. Ignore response, just send.
return True
def get_config(self, shutit):
return True
class ConnBash(ShutItConnModule):
"""Connects ShutIt to a machine via bash.
Assumes no docker daemon available for tagging and pushing.
"""
def is_installed(self, shutit):
"""Always considered false for ShutIt setup.
"""
return False
def get_config(self, shutit):
return True
def build(self, shutit):
"""Sets up the machine ready for building.
"""
shutit_pexpect_session = ShutItPexpectSession(shutit, 'target_child','/bin/bash')
target_child = shutit_pexpect_session.pexpect_child
shutit_pexpect_session.expect(shutit_global.shutit_global_object.base_prompt.strip(), timeout=10)
self.setup_host_child(shutit)
self.setup_target_child(shutit, target_child)
return True
def finalize(self, shutit):
"""Finalizes the target, exiting for us back to the original shell
and performing any repository work required.
"""
# Finish with the target
target_child_pexpect_session = shutit.get_shutit_pexpect_session_from_id('target_child')
assert not target_child_pexpect_session.sendline(ShutItSendSpec(target_child_pexpect_session,'exit',ignore_background=True)), shutit_util.print_debug()
return True
def conn_module():
"""Connects ShutIt to something
"""
return [
ConnDocker('shutit.tk.conn_docker', -0.1, description='Connect ShutIt to docker'),
ConnBash ('shutit.tk.conn_bash', -0.1, description='Connect ShutIt to a host via bash'),
]
class setup(ShutItModule):
def is_installed(self, shutit):
"""Always considered false for ShutIt setup.
"""
return False
def build(self, shutit):
"""Initializes target ready for build and updating package management if in container.
"""
if shutit.build['delivery'] in ('docker','dockerfile'):
if shutit.get_current_shutit_pexpect_session_environment().install_type == 'apt':
shutit.add_to_bashrc('export DEBIAN_FRONTEND=noninteractive')
if not shutit.command_available('lsb_release'):
shutit.install('lsb-release')
shutit.lsb_release()
elif shutit.get_current_shutit_pexpect_session_environment().install_type == 'yum':
# yum updates are so often "bad" that we let exit codes of 1 through.
# TODO: make this more sophisticated
shutit.send('yum update -y', timeout=9999, exit_values=['0', '1'])
shutit.pause_point('Anything you want to do to the target host ' + 'before the build starts?', level=2)
return True
def remove(self, shutit):
"""Removes anything performed as part of build.
"""
return True
def get_config(self, shutit):
"""Gets the configured core pacakges, and whether to perform the package
management update.
"""
return True
def module():
return setup('shutit.tk.setup', 0.0, description='Core ShutIt setup')