summaryrefslogtreecommitdiffstats
path: root/mvn-phase-script.sh
blob: bf761dce72418b9577731ead6a09f9b279b4d17f (plain)
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
#!/bin/bash

echo "running script: [$0] for module [$1] at stage [$2]"

MVN_MODULE=$1

echo "=> Prepare environment "
#env
if [ -z "$MVN_DOCKERREG_URL" ]; then
   MVN_DOCKERREG_URL='nexus3.onap.org:10001'
fi
if [ -z "$SETTINGS_FILE" ]; then
   SETTINGS_FILE='settings.xml'
fi


TIMESTAMP=$(date +%C%y%m%dT%H%M%S) 
export BUILD_NUMBER="${TIMESTAMP}"

# expected environment variables 
if [ -z "${MVN_NEXUSPROXY}" ]; then
    echo "MVN_NEXUSPROXY environment variable not set.  Cannot proceed"
    exit
fi
MVN_NEXUSPROXY_HOST=$(echo $MVN_NEXUSPROXY |cut -f3 -d'/' | cut -f1 -d':')


if [ -z "${SETTINGS_FILE}" ]; then
    echo "SETTINGS_FILE environment variable not set.  Cannot proceed"
    exit
fi

if [  ]; then

# login to all docker registries
DOCKER_REPOSITORIES="nexus3.onap.org:10001 nexus3.onap.org:10002 nexus3.onap.org:10003 nexus3.onap.org:10004"
for DOCKER_REPOSITORY in $DOCKER_REPOSITORIES;
do
    USER=$(xpath -e "//servers/server[id='$DOCKER_REPOSITORY']/username/text()" "$SETTINGS_FILE")
    PASS=$(xpath -e "//servers/server[id='$DOCKER_REPOSITORY']/password/text()" "$SETTINGS_FILE")

    if [ -z "$USER" ]; then
        echo "Error: no user provided"
    fi

    if [ -z "$PASS" ]; then
        echo "Error: no password provided"
    fi

    [ -z "$PASS" ] && PASS_PROVIDED="<empty>" || PASS_PROVIDED="<password>"
    echo docker login $DOCKER_REPOSITORY -u "$USER" -p "$PASS_PROVIDED"
    docker login $DOCKER_REPOSITORY -u "$USER" -p "$PASS"
done
fi

# ste up env variables, get ready for template resolution
export ONAPTEMPLATE_RAWREPOURL_org_onap_dcaegen2="${MVN_NEXUSPROXY}/content/sites/raw"
export ONAPTEMPLATE_PYPIURL_org_onap_dcaegen2="${MVN_NEXUSPROXY}/content/sites/pypi"
export ONAPTEMPLATE_DOCKERREGURL_org_onap_dcaegen2="${MVN_DOCKERREG_URL}"
export ONAPTEMPLATE_GITREPO_org_onap_dcaegen2="https://gerrit.onap.org"

# use the version text detect which phase we are in in LF CICD process: verify, merge, or (daily) release
LF_PHASE="verify"

#case "$phase" in
#    verify|merge ) REPO="${NEXUS_RAW}/org.openecomp.dcae.pgaas/deb-snapshots" ;;
#    release ) REPO="${NEXUS_RAW}/org.openecomp.dcae.pgaas/deb-releases" ;;
#esac

# mvn phase in life cycle 
MVN_PHASE="$2"

case $MVN_PHASE in
clean)
  echo "==> clean phase script"
  ;;
generate-sources)
  echo "==> generate-sources phase script. Dir is $(pwd)"

  TEMPLATES=$(env |grep ONAPTEMPLATE)
  echo "====> Resolving the following temaplate from environment variables "
  echo "[$TEMPLATES]"
  SELFFILE=$(echo $0 | rev | cut -f1 -d '/' | rev)
  for TEMPLATE in $TEMPLATES; do
    KEY=$(echo $TEMPLATE | cut -f1 -d'=')
    VALUE=$(echo $TEMPLATE | cut -f2 -d'=')
    VALUE2=$(echo $TEMPLATE | cut -f2 -d'=' |sed 's/\//\\\//g')
    FILES=$(grep -rl "$KEY")

    # assuming FILES is not longer than 2M bytes, the limit for variable value max size on this VM 
    for F in $FILES; do
       if [[ $F == *"$SELFFILE" ]]; then
          continue
       fi
       echo "======> Resolving template $KEY to value $VALUE for file $F"
       sed -i "s/{{[[:space:]]*$KEY[[:space:]]*}}/$VALUE2/g" $F
    done 
    
    #if [ ! -z "$FILES" ]; then
    #   echo "====> Resolving template $VALUE to value $VALUE"
    #   #CMD="grep -rl \"$VALUE\" | tr '\n' '\0' | xargs -0 sed -i \"s/{{[[:space:]]*$VALUE[[:space:]]*}}/$VALUE/g\""
    #   grep -rl "$KEY" | tr '\n' '\0' | xargs -0 sed -i 's/$KEY/$VALUE2/g'
    #   #echo $CMD
    #   #eval $CMD
    #fi
  done
  echo "====> Done template reolving"
  ;;
compile)
  echo "==> compile phase script"
  ;;
test)
  echo "==> test phase script"
  ;;
package)
  echo "==> package phase script"
  ;;
install)
  echo "==> install phase script"
  ;;
deploy)
  echo "==> deploy phase script"

  # prepare credential for curl use (raw repo)
  USER=$(xpath -q -e "//servers/server[id='ecomp-raw']/username/text()" "$SETTINGS_FILE")
  PASS=$(xpath -q -e "//servers/server[id='ecomp-raw']/password/text()" "$SETTINGS_FILE")
  export NETRC=$(mktemp)
  echo "machine $MVN_NEXUSPROXY_HOST login ${USER} password ${PASS}" >> "${NETRC}"
  set -x; curl -k --netrc-file '${NETRC}' --upload-file '{0}' '${REPO}/{2}-{1}'



  # login to all docker registries
  USER=$(xpath -e "//servers/server[id='$MVN_DOCKERREG_URL']/username/text()" "$SETTINGS_FILE")
  PASS=$(xpath -e "//servers/server[id='$MVN_DOCKERREG_URL']/password/text()" "$SETTINGS_FILE")
  if [ -z "$USER" ]; then
    echo "Error: no user provided"
  fi
  if [ -z "$PASS" ]; then
    echo "Error: no password provided"
  fi
  [ -z "$PASS" ] && PASS_PROVIDED="<empty>" || PASS_PROVIDED="<password>"
  echo docker login $MVN_DOCKERREG_URL -u "$USER" -p "$PASS_PROVIDED"
  docker login $MVN_DOCKERREG_URL -u "$USER" -p "$PASS"

  #docker push
  ;;
*)
  echo "==> unprocessed phase"
  ;;
esac