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:
    - $environment: $.find(std:Environment).require()
    - $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}

    - $environment.stack.updateTemplate($template)
    - Return: {get_resource: $userDataResourceName}


  generateCloudConfig:
    Body:
        - $resources: new(sys:Resources)
        - $muranoInitConf: $resources.yaml('murano-init.conf')
        - Return: $muranoInitConf

  generateUserData:
    Body:
        - $environment: $.find(std:Environment).require()
        - $rabbitMqParams: $environment.regionConfigs.get($environment.region).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_INPUT_QUEUE%": $.agent.queueName()
            "%RESULT_QUEUE%": $environment.agentListener.queueName()
        - $scriptReplacements:
            "%AGENT_CONFIG_BASE64%": base64encode($configFile.replace($configReplacements))
            "%INTERNAL_HOSTNAME%": $.name
            "%MURANO_SERVER_ADDRESS%": coalesce(config(file_server), $rabbitMqParams.host)
            "%CA_ROOT_CERT_BASE64%": ""
        - $muranoReplacements:
            "%MURANO_AGENT_CONF%": base64encode($muranoAgentConf)
            "%MURANO_AGENT_SERVICE%": base64encode($muranoAgentService)
            "%MURANO_AGENT%": base64encode($muranoAgent)

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



