Skip to content

Commit 5d78fb8

Browse files
author
Gabriel Monroy
committed
move default cloud init into FlavorManager
1 parent c516468 commit 5d78fb8

2 files changed

Lines changed: 13 additions & 12 deletions

File tree

api/models.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
from api import fields
2222
from celerytasks import chef, controller
2323
from celery.canvas import group
24+
import yaml
25+
import os.path
2426

2527

2628
# define custom signals
@@ -117,6 +119,14 @@ class Meta:
117119
@python_2_unicode_compatible
118120
class FlavorManager(models.Manager):
119121

122+
def load_cloud_config_base(self):
123+
# load cloud-config-base yaml_
124+
_cloud_config_path = os.path.abspath(
125+
os.path.join(__file__, '..', 'files', 'cloud-config-base.yml'))
126+
with open(_cloud_config_path) as f:
127+
_data = f.read()
128+
return yaml.safe_load(_data)
129+
120130
def seed(self, user, **kwargs):
121131
# TODO: add optimized AMIs to default flavors
122132
flavors = (
@@ -145,9 +155,11 @@ def seed(self, user, **kwargs):
145155
'provider': 'ec2',
146156
'params': json.dumps({'region': 'sa-east-1'})},
147157
)
158+
cloud_config = self.load_cloud_config_base()
148159
for flavor in flavors:
149160
provider = flavor.pop('provider')
150161
flavor['provider'] = Provider.objects.get(owner=user, id=provider)
162+
flavor['init'] = cloud_config
151163
self.create(owner=user, **flavor)
152164

153165

api/views.py

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@
1515
from rest_framework.response import Response
1616
from rest_framework.status import HTTP_400_BAD_REQUEST, HTTP_201_CREATED
1717
import json
18-
import os.path
19-
import yaml
20-
2118

2219
class AnonymousAuthentication(BaseAuthentication):
2320

@@ -128,14 +125,6 @@ class ProviderViewSet(OwnerViewSet):
128125
lookup_field = 'id'
129126

130127

131-
def _load_cloud_config_base():
132-
# load cloud-config-base yaml_
133-
_cloud_config_path = os.path.abspath(
134-
os.path.join(__file__, '..', 'files', 'cloud-config-base.yml'))
135-
with open(_cloud_config_path) as f:
136-
_data = f.read()
137-
return yaml.safe_load(_data)
138-
139128
class FlavorViewSet(OwnerViewSet):
140129

141130
model = models.Flavor
@@ -146,7 +135,7 @@ def create(self, request, **kwargs):
146135
request._data = request.DATA.copy()
147136
# set default cloud-init configuration
148137
if not 'init' in request.DATA:
149-
request.DATA['init'] = _load_cloud_config_base()
138+
request.DATA['init'] = models.FlavorManager().load_cloud_config_base()
150139
return viewsets.ModelViewSet.create(self, request, **kwargs)
151140

152141

0 commit comments

Comments
 (0)