~james-page/ubuntu-openstack-ci/drop-tox-from-slave-config

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
92
93
94
95
96
97
98
99
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
156
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
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
#!/bin/bash -e
#  Common functions for UOSCI layer ci, charm build, etc.

function f_layerci_use_built_asset_if_relevant(){
# Collect and extract built asset archive if relevant.
  if f_layerci_in_effect; then
    f_layerci_built_asset_var_sanity_checks
    f_layerci_scp_get_built_asset_archive_file "$BUILT_ASSET_ARTIFACT_ARCHIVE_NAME"
    f_layerci_validate_retrieved_asset_archive_file "$BUILT_ASSET_ARTIFACT_ARCHIVE_NAME" "$BUILT_ASSET_SHA1"
    f_layerci_extract_asset_archive_file "$BUILT_ASSET_ARTIFACT_ARCHIVE_NAME" "$BUILT_ASSET_BASE_NAME"
  fi
}


function f_layerci_in_effect(){
# Use env vars to determent if layerci (built assets) are in effect.
# Such env vars will exist via job parameters if relevant.
  if [[ -n "$BUILT_ASSET_ARTIFACT_ARCHIVE_NAME" ]] &&\
     [[ -n "$BUILT_ASSET_BASE_NAME" ]] &&\
     [[ -n "$BUILT_ASSET_SHA1" ]]; then
    return 0
  else
    # Not in effect.
    return 1
  fi
}


function f_layerci_built_asset_var_sanity_checks(){
# Make sure the built asset name matches the base name of the job.
# Fail and run far and fast if not.
  f_line "Sanity check on BUILT_ASSET_* values"
  if [[ -n "$BUILT_ASSET_BASE_NAME" ]] && [[ -n "$BASE_NAME" ]] &&\
     [[ "$BUILT_ASSET_BASE_NAME" != "$BASE_NAME" ]]; then
    echo " ! BUILT_ASSET_BASE_NAME does not match BASE_NAME.  Bailing!"
    env | grep BUILT_ASSET | sort
    exit 1
  else
    echo " . OK: BUILT_ASSET_BASE_NAME matches BASE_NAME."
    env | grep BUILT_ASSET | sort
  fi
}


function f_layerci_scp_get_built_asset_archive_file(){
# Retrieve a built asset archive file from shared artifact storage and
# place it in the workspace asset dir.  Takes 1 parameter: the archive filename.
  ARCHIVE_NAME="$1"
  f_line "Retrieving $ARCHIVE_NAME from artifact storage"
  if [[ -z "$ARCHIVE_NAME" ]]; then
    echo " ! Missing required parameter:  built asset artifact archive name"
    exit 1
  fi
  mkdir -p $WORKSPACE/asset
  scp -p jenkins@${OSCI_MASTER_IP}:/mnt/jenkins-ssh-artifact-storage/charm_build/$ARCHIVE_NAME $WORKSPACE/asset/$ARCHIVE_NAME
  stat $WORKSPACE/asset/$ARCHIVE_NAME
}


function f_layerci_validate_retrieved_asset_archive_file(){
# Validate hash sum of the retrieved archive file against the hash value passed
# from the earlier build job.
  ARCHIVE_NAME="$1"
  f_line "Validating $ARCHIVE_NAME"
  if [[ -z "$ARCHIVE_NAME" ]]; then
    echo " ! Missing required parameter:  built asset artifact archive name"
    exit 1
  fi
  ARCHIVE_SHA1="$2"
  if [[ -z "$ARCHIVE_SHA1" ]]; then
    echo " ! Missing required parameter:  artifact archive sha1"
    exit 1
  fi

  RETRIEVED_SHA1="$(cd $WORKSPACE/asset; sha1sum $ARCHIVE_NAME)"
  if [[ "$ARCHIVE_SHA1" == "$RETRIEVED_SHA1" ]]; then
    echo " . SHA1 check ok for: $WORKSPACE/asset/$ARCHIVE_NAME ($RETRIEVED_SHA1)"
  else
    echo " ! SHA1 check failed for: $WORKSPACE/asset/$ARCHIVE_NAME"
    echo "   Actual:   $RETRIEVED_SHA1"
    echo "   Expected: $ARCHIVE_SHA1"
    exit 1
  fi
}


function f_layerci_extract_asset_archive_file(){
# Extract/expand an asset archive file into the asset dir.
  f_line "Extracting $ARCHIVE_NAME to $WORKSPACE/asset/$ASSET_NAME"
  ARCHIVE_NAME="$1"
  ASSET_NAME="$2"
  if [[ -z "$ARCHIVE_NAME" ]]; then
    echo " ! Missing required parameter:  built asset artifact archive name"
    exit 1
  fi
  if [[ -z "$ASSET_NAME" ]]; then
    echo " ! Missing required parameter:  asset name (ie. charm name)"
    exit 1
  fi
  mkdir -p $WORKSPACE/asset/$ASSET_NAME
  tar -jxvf $WORKSPACE/asset/$ARCHIVE_NAME -C $WORKSPACE/asset/$ASSET_NAME
  ls -al $WORKSPACE/asset/$ASSET_NAME
  export BUILT_ASSET_DIR=$WORKSPACE/asset/$ASSET_NAME
}


function f_layerci_get_asset_type(){
# Takes 1 required parameter: the check out path.
# Sets env vars to be consumed by jobs.
  TMP_DIR="$1"
  if [[ -z "$TMP_DIR" ]]; then
    echo " ! Missing required parameter:  checkout dir"
    exit 1
  fi
  f_line "Checking asset type of $TMP_DIR"

  ${UOSCI_BOT_CONTROL_ROOT}/tools/charm-what $TMP_DIR  # Exits non-zero if unable to determine type
  export OSCI_ASSET_TYPE="$(${UOSCI_BOT_CONTROL_ROOT}/tools/charm-what $TMP_DIR)"

  if [[ -z "$OSCI_ASSET_TYPE" ]]; then
    # Unlikely to hit this; trap anyway.
    echo " ! Unable to determine asset type for $TMP_DIR"
    exit 1
  fi
}


function f_layerci_build_charm(){
# Build asset (charm) based on checkout dir and base name env vars,
# and set the built asset dir env var to be consumed by jobs.
  if [[ -z "$CO_DIR" ]]; then
    echo " ! Missing required CO_DIR env var"
    exit 1
  fi
  if [[ -z "$BASE_NAME" ]]; then
    echo " ! Missing required BASE_NAME env var"
    exit 1
  fi
  f_line "Building $CO_DIR ($BASE_NAME)"

  # Build the asset
  f_proxy_if_proxy
  . $UOSCI_RELEASE_TOOLS_ROOT/build-charm $CO_DIR
  f_proxy_unset

  # Confirm built asset location
  if [[ -z "$BUILT_ASSET_DIR" ]]; then
    echo " ! Unable to determine BUILT_ASSET_DIR"
    exit 1
  fi
  echo " . BUILT_ASSET_DIR: $BUILT_ASSET_DIR"
  du -hsc $BUILT_ASSET_DIR

  # Inject repo summary into the built asset
  $UOSCI_RELEASE_TOOLS_ROOT/generate-repo-info $CO_DIR &> $BUILT_ASSET_DIR/repo-info
}


function f_layerci_archive_built_asset(){
# Assumes the asset (charm) has already been built.
# Sets the properties files for later jobs to consume.
  f_line "Archiving built asset $BUILT_ASSET_DIR as artifact"
  if [[ -z "$BUILT_ASSET_DIR" ]]; then
    echo " ! Missing required env var BUILT_ASSET_DIR"
    exit 1
  fi
  if [[ ! -d "$BUILT_ASSET_DIR" ]]; then
    echo " ! BUILT_ASSET_DIR does not exist"
    exit 1
  fi

  # Create archive and make sure it exists
  # Builds always go in workspace/build/
  mkdir -p $WORKSPACE/build
  export BUILT_ASSET_ARTIFACT_ARCHIVE_NAME="${BASE_NAME}-${JOB_NAME}-${BUILD_NUMBER}.tar.bz2"
  tar -C $BUILT_ASSET_DIR -cjvf $WORKSPACE/build/$BUILT_ASSET_ARTIFACT_ARCHIVE_NAME .
  ls -alh $WORKSPACE/build/$BUILT_ASSET_ARTIFACT_ARCHIVE_NAME

  # Create properties files for parameterized job consumption. These env vars
  # are critical to pass along for layer ci awareness in other jobs.
  echo "BUILT_ASSET_ARTIFACT_ARCHIVE_NAME=$BUILT_ASSET_ARTIFACT_ARCHIVE_NAME" > $WORKSPACE/charm_build.properties
  echo "BUILT_ASSET_BASE_NAME=$BASE_NAME" >> $WORKSPACE/charm_build.properties
  echo "BUILT_ASSET_SHA1=$(cd $WORKSPACE/build; sha1sum $BUILT_ASSET_ARTIFACT_ARCHIVE_NAME)" >> $WORKSPACE/charm_build.properties
}


function f_layerci_build_and_archive_if_necessary() {
# Detect asset (charm/layer/interface) type, build and archive if relevant.
# Announce detection for clarity.  Expects CO_DIR and OSCI_ASSET_TYPE
# env vars to already be set.
  case "$OSCI_ASSET_TYPE" in
    "charm (built)")
        # Change nothing at this time
        echo " . Proceeding with $CO_DIR as an already-built charm [no build necessary]"
        ;;
    "charm (classic)")
        # Change nothing at this time
        echo " . Proceeding with $CO_DIR as a classic (non-layered) charm [no build necessary]"
        ;;
    "charm (source)")
        # Build and prepare jenkins workspace artifact for copying by other jobs
        echo " . Proceeding with $CO_DIR as a source charm [need to build]"
        f_layerci_build_charm $CO_DIR
        f_layerci_archive_built_asset
        ;;
    "layer")
        # Build and prepare jenkins workspace artifact for copying by other jobs
        echo " . Proceeding with $CO_DIR as a layer [need to build]"
        f_layerci_build_charm $CO_DIR
        f_layerci_archive_built_asset
        ;;
    "interface")
        # Change nothing at this time
        echo " . Proceeding with $CO_DIR as an interface [no build necessary]"
        ;;
    *)
        echo " ! Unknown asset type"
        exit 1
        ;;
  esac
}


function f_layerci_fail_if_not_deployable_type(){
# This is used in front of deployment tests, such as charm single,
# func tests and such, to not waste time bootstrapping and attempting
# to deploy an unbuilt layer or interface. Those jobs should never
# attempt that. This makes sure and indicates if/when they do try.

  f_layerci_check_and_announce_asset_type
  case "$OSCI_ASSET_TYPE" in
    "charm (built)"|"charm (classic)")
        # OK
        :
        ;;
    "charm (source)"|"layer"|"interface")
        # Bail!
        echo " ! Refusing to attempt a deploy test with a not-deployable asset: $OSCI_ASSET_TYPE"
        exit 1
        ;;
    *)
        #
        echo " ! Unknown asset type"
        exit 1
        ;;
  esac
}


function f_layerci_build_if_necessary() {
    f_layerci_check_and_announce_asset_type
    case "$OSCI_ASSET_TYPE" in
        "charm (built)"|"charm (classic)")
            # OK
            :
            ;;
        "charm (source)")
            f_layerci_build_charm
            ;;
        *)
            #
            echo " ! Asset type not supported in this call: $OSCI_ASSET_TYPE"
            exit 1
        ;;
    esac
}


function f_layerci_check_and_announce_asset_type() {
# Check (or re-check) and announce asset type via charm-what.
  f_layerci_get_asset_type ${BUILT_ASSET_DIR-$CO_DIR}
  case "$OSCI_ASSET_TYPE" in
    "charm (built)"|"charm (classic)"|"charm (source)"|"layer"|"interface")
        # OK
        :
        ;;
    *)
        echo " ! Unknown asset type"
        exit 1
        ;;
  esac
}