Namespaces:
  =: io.murano.resources
  sys: io.murano.system
  std: io.murano

Name: LinuxMuranoInstance

Extends:
  - LinuxInstance

Methods:
  prepareUserData:
    Body:
     - $userData: $.generateUserData()
     - Return:
        data: $._generateInstanceConfigResources($userData)
        format: RAW

  _generateInstanceConfigResources:
    Arguments:
    - userData:
        Contract: $.string().notNull()
    Body:
    - $region: $.getRegion()
    - $resources: new(sys:Resources)
    - $muranoInitConf: $.generateCloudConfig()
    - $bootScriptResourceName: format('boot_script_{0}', $.name)
    - $userDataResourceName: format('user_data-{0}', $.name)
    - $bootConfigResourceName: format('boot_config-{0}', $.name)
    - $template:
          resources:
            $bootConfigResourceName:
              type: 'OS::Heat::CloudConfig'
              properties:
                cloud_config: $muranoInitConf
            $bootScriptResourceName:
              type: 'OS::Heat::SoftwareConfig'
              properties:
                group: ungrouped
                config: $userData
            $userDataResourceName:
              type: 'OS::Heat::MultipartMime'
              properties:
                parts:
                - config: {get_resource: $bootConfigResourceName}
                - config: {get_resource: $bootScriptResourceName}

    - $.setAttr(resourceCloudConfig, [$bootScriptResourceName, $userDataResourceName, $bootConfigResourceName])
    - $region.stack.updateTemplate($template)
    - Return: {get_resource: $userDataResourceName}


  generateCloudConfig:
    Body:
        - $resources: new(sys:Resources)
        - $muranoInitConf: $resources.yaml('murano-init.conf').bind(dict(
                instanceHostname  => $.name
              ))
        - Return: $muranoInitConf

  generateUserData:
    Body:
        - $region: $.getRegion()
        - $rabbitMqParams: $region.getConfig().agentRabbitMq
        - $resources: new(sys:Resources)
        - $configFile: $resources.string('Agent-v2.template')
        - $initScript: $resources.string('linux-init.sh')
        - $muranoScript: $resources.string('murano-init.sh')
        - $muranoAgentConf: $resources.string('murano-agent.conf')
        - $muranoAgentService: $resources.string('murano-agent.service')
        - $muranoAgent: $resources.string('murano-agent')
        - $configReplacements:
            "%RABBITMQ_HOST%": $rabbitMqParams.host
            "%RABBITMQ_PORT%": $rabbitMqParams.port
            "%RABBITMQ_USER%": $rabbitMqParams.login
            "%RABBITMQ_PASSWORD%": $rabbitMqParams.password
            "%RABBITMQ_VHOST%": $rabbitMqParams.virtual_host
            "%RABBITMQ_SSL%": str($rabbitMqParams.ssl).toLower()
            "%RABBITMQ_INSECURE%": str($rabbitMqParams.insecure).toLower()
            "%RABBITMQ_INPUT_QUEUE%": $.agent.queueName()
            "%RESULT_QUEUE%": $region.agentListener.queueName()
            "%SIGNING_KEY%": $.agent.signingKey('\t')
        - $scriptReplacements:
            "%AGENT_CONFIG_BASE64%": base64encode($configFile.replace($configReplacements))
            "%INTERNAL_HOSTNAME%": $.name
            "%MURANO_SERVER_ADDRESS%": coalesce(config(file_server), $rabbitMqParams.host)
        - If: config(rabbitmq, ca_certs)
          Then:
            - $scriptReplacements["%CA_ROOT_CERT_BASE64%"]: base64encode(config(rabbitmq, ca_certs, true))
          Else:
            - $scriptReplacements["%CA_ROOT_CERT_BASE64%"]: ''
        - $muranoReplacements:
            "%MURANO_AGENT_CONF%": base64encode($muranoAgentConf)
            "%MURANO_AGENT_SERVICE%": base64encode($muranoAgentService)
            "%MURANO_AGENT%": base64encode($muranoAgent)
            "%PIP_SOURCE%": config(engine, agent_source)

        - $userData: $muranoScript.replace($muranoReplacements) + $initScript.replace($scriptReplacements)
        - Return: $userData

  releaseResources:
    Body:
      - $region: $.getRegion()
      - $template: $region.stack.current()
      - If: $template.get(resources) and $template.get(outputs)
        Then:
          - $resourcesToDelete: $.getAttr(resourceCloudConfig, [])
          - $template.resources: $template.resources.deleteAll($resourcesToDelete)
          - $region.stack.setTemplate($template)

      - super($, $.releaseResources())
