2014-05-02 17:36:08 +02:00
|
|
|
from bootstrapvz.base import Task
|
|
|
|
from bootstrapvz.common import phases
|
2015-10-16 00:41:53 -03:00
|
|
|
from bootstrapvz.common.tasks import image
|
2014-05-02 17:36:08 +02:00
|
|
|
from bootstrapvz.common.tools import log_check_call
|
|
|
|
import os.path
|
|
|
|
|
|
|
|
|
|
|
|
class CreateTarball(Task):
|
|
|
|
description = 'Creating tarball with image'
|
|
|
|
phase = phases.image_registration
|
2015-10-16 00:41:53 -03:00
|
|
|
predecessors = [image.MoveImage]
|
2014-05-02 17:36:08 +02:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def run(cls, info):
|
|
|
|
import datetime
|
2015-12-13 14:54:06 +01:00
|
|
|
image_name = info.manifest.name.format(**info.manifest_vars)
|
2014-05-03 22:24:13 +02:00
|
|
|
filename = image_name + '.' + info.volume.extension
|
2014-05-02 17:36:08 +02:00
|
|
|
today = datetime.datetime.today()
|
|
|
|
name_suffix = today.strftime('%Y%m%d')
|
|
|
|
image_name_format = '{lsb_distribution}-{lsb_release}-{release}-v{name_suffix}'
|
|
|
|
image_name = image_name_format.format(lsb_distribution=info._gce['lsb_distribution'],
|
|
|
|
lsb_release=info._gce['lsb_release'],
|
|
|
|
release=info.manifest.system['release'],
|
|
|
|
name_suffix=name_suffix)
|
|
|
|
# ensure that we do not use disallowed characters in image name
|
|
|
|
image_name = image_name.lower()
|
|
|
|
image_name = image_name.replace(".", "-")
|
|
|
|
info._gce['image_name'] = image_name
|
2014-05-03 22:24:13 +02:00
|
|
|
tarball_name = image_name + '.tar.gz'
|
2014-05-02 17:36:08 +02:00
|
|
|
tarball_path = os.path.join(info.manifest.bootstrapper['workspace'], tarball_name)
|
|
|
|
info._gce['tarball_name'] = tarball_name
|
|
|
|
info._gce['tarball_path'] = tarball_path
|
|
|
|
log_check_call(['tar', '--sparse', '-C', info.manifest.bootstrapper['workspace'],
|
|
|
|
'-caf', tarball_path, filename])
|
|
|
|
|
|
|
|
|
|
|
|
class UploadImage(Task):
|
2015-10-18 16:31:12 -02:00
|
|
|
description = 'Uploading image to GCS'
|
2014-05-02 17:36:08 +02:00
|
|
|
phase = phases.image_registration
|
|
|
|
predecessors = [CreateTarball]
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def run(cls, info):
|
|
|
|
log_check_call(['gsutil', 'cp', info._gce['tarball_path'],
|
2015-12-13 14:54:06 +01:00
|
|
|
info.manifest.provider['gcs_destination'] + info._gce['tarball_name']])
|
2014-05-02 17:36:08 +02:00
|
|
|
|
|
|
|
|
|
|
|
class RegisterImage(Task):
|
|
|
|
description = 'Registering image with GCE'
|
|
|
|
phase = phases.image_registration
|
|
|
|
predecessors = [UploadImage]
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def run(cls, info):
|
|
|
|
image_description = info._gce['lsb_description']
|
2015-12-13 14:54:06 +01:00
|
|
|
if 'description' in info.manifest.provider:
|
|
|
|
image_description = info.manifest.provider['description']
|
2016-04-17 12:00:44 -04:00
|
|
|
image_description = image_description.format(**info.manifest_vars)
|
2015-12-15 11:20:49 -08:00
|
|
|
log_check_call(['gcloud', 'compute', '--project=' + info.manifest.provider['gce_project'],
|
2016-04-17 11:59:25 -04:00
|
|
|
'images', 'create', info._gce['image_name'],
|
|
|
|
'--source-uri=' + info.manifest.provider['gcs_destination'] + info._gce['tarball_name'],
|
2014-05-03 22:24:13 +02:00
|
|
|
'--description=' + image_description])
|