so.py 11.5 KB
Newer Older
Balazs's avatar
Balazs committed
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
#   Copyright 2015 Zuercher Hochschule fuer Angewandte Wissenschaften
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.

import os

from sdk.mcn import util
from sm.so import service_orchestrator
from sm.so.service_orchestrator import LOG


# TODO replace print statements with LOG

class SOE(service_orchestrator.Execution):

    def __init__(self, token, tenant):
        LOG.info( "initializing stack")

        super(SOE, self).__init__(token, tenant)
        self.token = token
        self.tenant = tenant

        self.hadoop_master = None
        self.deployer = util.get_deployer(self.token,
                                          url_type='public',
36
                                          tenant_name=self.tenant)
Balazs's avatar
Balazs committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55

        LOG.info("token = "+self.token)
        LOG.info("tenant = "+self.tenant)

    def design(self):
        print( "designing stack" );

        """
        Do initial design steps here.
        """
        LOG.info('Entered design() - nothing to do here')
        # self.resolver.design()

    def deploy(self, attributes=None):

        LOG.info("the attributes are:")
        LOG.info(attributes)
        print( "deploying stack")

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
        masterCount = 1
        slaveCount = 1
        slaveOnMaster = True
        clusterName = "DistributedProcessing"
        masterImage = "Ubuntu-Trusty-1404-7-10-2015"
        slaveImage = "Ubuntu-Trusty-1404-7-10-2015"
        sshKeyName = "mesz MNMBA"
        sshPublicKey = ""
        processingFramework = None
        masterFlavor = "m1.small"
        slaveFlavor = "m1.small"


        if 'icclab.haas.master.number' in attributes:
            masterCount = int(attributes['icclab.haas.master.number'])
        if 'icclab.haas.slave.number' in attributes:
            slaveCount = int(attributes['icclab.haas.slave.number'])
        if 'icclab.haas.cluster.name' in attributes:
            clusterName = attributes['icclab.haas.cluster.name']
        if 'icclab.haas.master.image' in attributes:
            masterImage = attributes['icclab.haas.master.image']
        if 'icclab.haas.slave.image' in attributes:
            slaveImage = attributes['icclab.haas.slave.image']
        if 'icclab.haas.master.flavor' in attributes:
            masterFlavor = attributes['icclab.haas.master.flavor']
        if 'icclab.haas.slave.flavor' in attributes:
            slaveFlavor = attributes['icclab.haas.slave.flavor']
        if 'icclab.haas.master.slaveonmaster' in attributes:
            slaveOnMaster = attributes['icclab.haas.master.slaveonmaster'] in ['true', '1']
        if 'icclab.haas.master.sshkeyname' in attributes:
            sshKeyName = attributes['icclab.haas.master.sshkeyname']
        if 'icclab.haas.master.publickey' in attributes:
            sshPublicKey = attributes['icclab.haas.master.publickey']
        if 'icclab.haas.cluster.procframework' in attributes:
            processingFramework = attributes['icclab.haas.cluster.procframework']

Balazs's avatar
Balazs committed
92 93 94 95 96 97 98
        LOG.info('Calling deploy')
        LOG.debug('Executing deployment logic')

        # compile heat template and deploy it

        # TODO this will fail! :-) Fix
        from sm.so.service_orchestrator import BUNDLE_DIR
99
        rootFolder = "/app/data/"
Balazs's avatar
Balazs committed
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

        f = open(rootFolder + "cluster.yaml")
        clusterTemplate = f.read()
        f.close()

        f = open(rootFolder + "slave.yaml")
        slaveTemplate = f.read()
        f.close()

        f = open(rootFolder + "master_bash.sh")
        masterBash = f.read()
        f.close()

        f = open(rootFolder + "master.id_rsa")
        master_id_rsa = f.read()
        f.close()
        master_id_rsa = master_id_rsa.replace("\n","\\n")

        f = open(rootFolder + "master.id_rsa.pub")
        master_id_rsa_pub = f.read().replace("\n","")
        f.close()

        f = open(rootFolder + "yarn-site.xml")
        yarn_site_xml = f.read()
        f.close()

        f = open(rootFolder + "core-site.xml")
        core_site_xml = f.read()
        f.close()

        f = open(rootFolder + "mapred-site.xml")
        mapred_site_xml = f.read()
        f.close()

        f = open(rootFolder + "hdfs-site.xml")
        hdfs_site_xml = f.read()
        f.close()

        f = open(rootFolder + "hadoop-env.sh")
        hadoop_env_sh = f.read()
        f.close()

        slaves = ""
        hostFileContent = ""
        forLoopSlaves = ""
        paramsSlave = ""
        slavesFile = ""
        hostsListFile = ""

        master_name = "masternode"
        slave_name = "slavenode"

        subnet_cidr = "192.168.19.0/24"
        subnet_gw_ip = "192.168.19.1"
        subnet_allocation_pool_start = "192.168.19.2"
        subnet_allocation_pool_end = "192.168.19.254"
156
        subnet_dns_servers = '["64.6.64.6","64.6.65.6"]'
Balazs's avatar
Balazs committed
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

        slaveTemplate = slaveTemplate.replace("$master.id_rsa.pub$",master_id_rsa_pub)
        for i in xrange(1,slaveCount+1):
            slaves = slaves + slaveTemplate.replace("$slavenumber$",str(i))
            hostFileContent = hostFileContent + "$slave"+str(i)+"address$\t"+slave_name+str(i)+"\n"
            forLoopSlaves = forLoopSlaves + " $slave"+str(i)+"address$"
            paramsSlave = paramsSlave + "            $slave"+str(i)+"address$: { get_attr: [hadoop_slave_"+str(i)+", first_address] }\n"
            slavesFile = slavesFile + slave_name+str(i)+"\n"
            hostsListFile = hostsListFile + "$slave"+str(i)+"address$\n"

        # setup bash script for master
        masterBash = masterBash.replace("$master.id_rsa$",master_id_rsa)
        masterBash = masterBash.replace("$master.id_rsa.pub$",master_id_rsa_pub)
        masterBash = masterBash.replace("$yarn-site.xml$",yarn_site_xml)
        masterBash = masterBash.replace("$core-site.xml$",core_site_xml)
        masterBash = masterBash.replace("$mapred-site.xml$",mapred_site_xml)
        masterBash = masterBash.replace("$hdfs-site.xml$",hdfs_site_xml)
        masterBash = masterBash.replace("$hadoop-env.sh$",hadoop_env_sh)
        masterBash = masterBash.replace("$slavesfile$",slavesFile)
        masterBash = masterBash.replace("$hostsfilecontent$",hostFileContent)
        masterBash = masterBash.replace("$forloopslaves$",forLoopSlaves)
        masterBash = masterBash.replace("$jdk_uri$","http://download.oracle.com/otn-pub/java/jdk/8u60-b27/jdk-8u60-linux-x64.tar.gz")
        masterBash = masterBash.replace("$hadoop_uri$","http://mirror.switch.ch/mirror/apache/dist/hadoop/common/hadoop-2.7.1/hadoop-2.7.1.tar.gz")
        masterBash = masterBash.replace("$for_loop_slaves$",hostsListFile)
        masterBashLines = masterBash.splitlines(True)
        masterbash = ""
        for line in masterBashLines:
            masterbash = masterbash+"              "+line

        clusterTemplate = clusterTemplate.replace("$master_bash.sh$",masterbash)
        clusterTemplate = clusterTemplate.replace("$paramsslave$",paramsSlave)
        clusterTemplate = clusterTemplate.replace("$slaves$",slaves)
189 190
        clusterTemplate = clusterTemplate.replace("$master_image$",masterImage)
        clusterTemplate = clusterTemplate.replace("$slave_image$",slaveImage)
Balazs's avatar
Balazs committed
191 192
        clusterTemplate = clusterTemplate.replace("$masternode$",master_name)
        clusterTemplate = clusterTemplate.replace("$slavenode$",slave_name)
193 194 195 196 197 198 199 200 201 202 203
        clusterTemplate = clusterTemplate.replace("$master_flavor$",masterFlavor)
        clusterTemplate = clusterTemplate.replace("$slave_flavor$",slaveFlavor)

        if sshPublicKey!="":
            # create new public key
            print "not empty"
        else:
            # only enter existing SSH key's name
            print "empty"

        clusterTemplate = clusterTemplate.replace("$ssh_key$",sshKeyName) # was ssh_key
Balazs's avatar
Balazs committed
204 205 206 207 208 209
        clusterTemplate = clusterTemplate.replace("$subnet_cidr$",subnet_cidr)
        clusterTemplate = clusterTemplate.replace("$subnet_gw_ip$",subnet_gw_ip)
        clusterTemplate = clusterTemplate.replace("$subnet_allocation_pool_start$",subnet_allocation_pool_start)
        clusterTemplate = clusterTemplate.replace("$subnet_allocation_pool_end$",subnet_allocation_pool_end)
        clusterTemplate = clusterTemplate.replace("$subnet_dns_servers$",subnet_dns_servers)

210 211 212 213
        # TODO will fail - fix
#        f = open( "/Users/puenktli/Desktop/currentTemplate.yaml","w")
#        f.write( clusterTemplate )
#        f.close()
Balazs's avatar
Balazs committed
214

215
#        LOG.debug(clusterTemplate)
Balazs's avatar
Balazs committed
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
        LOG.debug(clusterTemplate)
        self.deployTemplate = clusterTemplate

        LOG.info("now, i'm starting the deployment...")
        if self.hadoop_master is None:
            self.hadoop_master = self.deployer.deploy(self.deployTemplate, self.token)
            LOG.info('Hadoop stack ID: ' + self.hadoop_master)

    def provision(self, attributes=None):
        print( "provisioning stack" );

        """
        provision the SICs on burns and ubern
        """

        LOG.info('Calling provision...')
        LOG.info('nothing to be done to be honest...')
        LOG.debug('Executing resource provisioning logic')
        # once logic executes, deploy phase is done

    def dispose(self):
        print( "disposal of stack" );

        """
        Dispose SICs on burns and ubern
        """
        LOG.info('Calling dispose')
        # self.resolver.dispose()

        if self.hadoop_master is not None:
            LOG.debug('Deleting stack: ' + self.hadoop_master)
            self.deployer.dispose(self.hadoop_master, self.token)
            self.hadoop_master = None

    def state(self):
        print( "getting status of stack")

        """
        Report on state for both stacks in burns and ubern
        """
        if self.hadoop_master is not None:
            tmp = self.deployer.details(self.hadoop_master, self.token)
            if 'output' not in tmp:
                return tmp['state'], self.hadoop_master, dict()
            return tmp['state'], self.hadoop_master, tmp['output']
        else:
            return 'Unknown', 'N/A', {}

    def update(self, old, new, extras):
        print( "updating stack")

        # TODO implement your own update logic - this could be a heat template
        # update call
        LOG.info('Calling update - nothing to do!')

    def notify(self, entity, attributes, extras):
        print( "notifying stack")

        super(SOE, self).notify(entity, attributes, extras)
        # TODO here you can add logic to handle a notification event sent by the CC
        # XXX this is optional


class SOD(service_orchestrator.Decision):
    """
    Sample Decision part of SO.
    """

    def __init__(self, so_e, token, tenant):
        super(SOD, self).__init__(so_e, token, tenant)
        self.so_e = so_e
        self.token = token
        self.tenant = tenant

    def run(self):
        """
        Decision part implementation goes here.
        """
        pass

    def stop(self):
        pass


class ServiceOrchestrator(object):
    """
    Sample SO.
    """

    def __init__(self, token, tenant):
        self.so_e = SOE(token=token, tenant=tenant)
        self.so_d = SOD(so_e=self.so_e, tenant=tenant, token=token)

"""
if __name__ == '__main__':

    token = '24289e2e5f2648bb8e0459505bfb14ca'
    tenant = 'mesz'

    soe = SOE(token, tenant)
    soe.design()
    soe.deploy()
    soe.provision()
"""