|
| 1 | +properties([ |
| 2 | + parameters([ |
| 3 | + [ |
| 4 | + $class: 'CascadeChoiceParameter', |
| 5 | + choiceType: 'PT_SINGLE_SELECT', |
| 6 | + name: 'Account', |
| 7 | + description: 'Select the account', |
| 8 | + script: [ |
| 9 | + $class: 'GroovyScript', |
| 10 | + fallbackScript: [ |
| 11 | + classpath: [], |
| 12 | + sandbox: true, |
| 13 | + script: 'return ["ERROR: Failed to fetch accounts"]' |
| 14 | + ], |
| 15 | + script: [ |
| 16 | + classpath: [], |
| 17 | + sandbox: true, |
| 18 | + script: ''' |
| 19 | + import groovy.json.JsonSlurper |
| 20 | +
|
| 21 | + try { |
| 22 | + def url = new URL('https://toaster.altuhov.su/api/dimension/example-org/account?workspace=master&fallbacktomaster=true&needdimdata=false') |
| 23 | + def connection = url.openConnection() |
| 24 | + connection.setRequestProperty('Authorization', 'Basic NjYyY2FiN2M1ZTExNjgxOTczOGIwMWZlOnN1cGVydG9hc3Rlcg==') |
| 25 | + connection.requestMethod = 'GET' |
| 26 | + |
| 27 | + def response = connection.content.text |
| 28 | + def result = new JsonSlurper().parseText(response) |
| 29 | + |
| 30 | + if (result.Dimensions) { |
| 31 | + return result.Dimensions.collect { it.DimValue }.unique() |
| 32 | + } else { |
| 33 | + return ["No accounts found"] |
| 34 | + } |
| 35 | + } catch (Exception e) { |
| 36 | + return ["Error: ${e.message}"] |
| 37 | + } |
| 38 | + ''' |
| 39 | + ] |
| 40 | + ] |
| 41 | + ], |
| 42 | + [ |
| 43 | + $class: 'CascadeChoiceParameter', |
| 44 | + choiceType: 'PT_SINGLE_SELECT', |
| 45 | + name: 'DataCenter', |
| 46 | + description: 'Select the Data Center', |
| 47 | + script: [ |
| 48 | + $class: 'GroovyScript', |
| 49 | + fallbackScript: [ |
| 50 | + classpath: [], |
| 51 | + sandbox: true, |
| 52 | + script: 'return ["ERROR: Failed to fetch data centers"]' |
| 53 | + ], |
| 54 | + script: [ |
| 55 | + classpath: [], |
| 56 | + sandbox: true, |
| 57 | + script: ''' |
| 58 | + import groovy.json.JsonSlurper |
| 59 | +
|
| 60 | + try { |
| 61 | + def url = new URL('https://toaster.altuhov.su/api/dimension/example-org/datacenter?workspace=master&fallbacktomaster=true&needdimdata=false') |
| 62 | + def connection = url.openConnection() |
| 63 | + connection.setRequestProperty('Authorization', 'Basic NjYyY2FiN2M1ZTExNjgxOTczOGIwMWZlOnN1cGVydG9hc3Rlcg==') |
| 64 | + connection.requestMethod = 'GET' |
| 65 | + |
| 66 | + def response = connection.content.text |
| 67 | + def result = new JsonSlurper().parseText(response) |
| 68 | + |
| 69 | + if (result.Dimensions) { |
| 70 | + return result.Dimensions.collect { it.DimValue }.unique() |
| 71 | + } else { |
| 72 | + return ["No accounts found"] |
| 73 | + } |
| 74 | + } catch (Exception e) { |
| 75 | + return ["Error: ${e.message}"] |
| 76 | + } |
| 77 | + ''' |
| 78 | + ] |
| 79 | + ] |
| 80 | + ] |
| 81 | + ]) |
| 82 | +]) |
| 83 | + |
| 84 | +pipeline { |
| 85 | + agent any |
| 86 | + stages { |
| 87 | + stage('Build') { |
| 88 | + steps { |
| 89 | + git url: 'https://github.com/alt-dima/tofugu.git', branch: 'main' |
| 90 | + echo "Selected account: ${params.Account}" |
| 91 | + echo "Selected data center: ${params.DataCenter}" |
| 92 | + } |
| 93 | + } |
| 94 | + stage('Install OpenTofu and TofuGu') { |
| 95 | + steps { |
| 96 | + script { |
| 97 | + def arch = sh(script: 'uname -m', returnStdout: true).trim() |
| 98 | + def archSuffix |
| 99 | + if (arch == 'aarch64' || arch == 'arm64') { |
| 100 | + archSuffix = 'arm64' |
| 101 | + } else if (arch == 'x86_64') { |
| 102 | + archSuffix = 'amd64' |
| 103 | + } else { |
| 104 | + error "Unsupported architecture: ${arch}" |
| 105 | + } |
| 106 | + |
| 107 | + sh "curl -L -o tofu_1.10.7_linux_${archSuffix}.tar.gz https://github.com/opentofu/opentofu/releases/download/v1.10.7/tofu_1.10.7_linux_${archSuffix}.tar.gz" |
| 108 | + sh "tar -xvf tofu_1.10.7_linux_${archSuffix}.tar.gz -C /tmp" |
| 109 | + |
| 110 | + sh "curl -L -o tofugu_0.5.1_linux_${archSuffix}.tar.gz https://github.com/alt-dima/tofugu/releases/download/v0.5.1/tofugu_0.5.1_linux_${archSuffix}.tar.gz" |
| 111 | + sh "tar -xvf tofugu_0.5.1_linux_${archSuffix}.tar.gz" |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + stage('Create .tofugu config') { |
| 116 | + steps { |
| 117 | + writeFile file: '.tofugu', text: ''' |
| 118 | +defaults: |
| 119 | + tofies_path: examples/tofies |
| 120 | + inventory_path: examples/inventory |
| 121 | + cmd_to_exec: /tmp/tofu |
| 122 | +''' |
| 123 | + } |
| 124 | + } |
| 125 | + stage('TofuGu Plan') { |
| 126 | + environment { |
| 127 | + toasterurl = 'https://662cab7c5e116819738b01fe:supertoaster@toaster.altuhov.su' |
| 128 | + } |
| 129 | + steps { |
| 130 | + ansiColor('xterm') { |
| 131 | + sh "./tofugu cook -o example-org -d account:${params.Account} -d datacenter:${params.DataCenter} -t demo -- init" |
| 132 | + sh "./tofugu cook -o example-org -d account:${params.Account} -d datacenter:${params.DataCenter} -t demo -- plan" |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + stage('Approval') { |
| 137 | + steps { |
| 138 | + input 'Do you want to apply the changes?' |
| 139 | + } |
| 140 | + } |
| 141 | + stage('TofuGu Apply') { |
| 142 | + environment { |
| 143 | + toasterurl = 'https://662cab7c5e116819738b01fe:supertoaster@toaster.altuhov.su' |
| 144 | + } |
| 145 | + steps { |
| 146 | + ansiColor('xterm') { |
| 147 | + sh "./tofugu cook -o example-org -d account:${params.Account} -d datacenter:${params.DataCenter} -t demo -- apply -auto-approve" |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | +} |
0 commit comments