IK.AM

@making's tech note


Nexus Repository ManagerのBOSH Releaseを作る

🗃 {Dev/Infrastructure/BOSH}
🏷 BOSH 🏷 Nexus Repository 
🗓 Updated at 2017-06-04T16:46:09Z  🗓 Created at 2017-03-06T18:27:29Z   🌎 English Page

Nexus Repository ManagerのBOSH Releaseを作ってみた。

JavaアプリのBOSH Releaseの作り方のメモとして残しておく。

最終的な成果物はhttps://github.com/making/nexus-boshreleaseに。

この例だとPersistent Diskを使っていないのでVMを作り直すとNexusのデータが無くなる。

Persistent Diskを使うようにあとで更新する

目次

  * [Pacakgeの作成](#pacakgeの作成)
    * [Java](#java)
    * [Nexus](#nexus)
  * [Job](#job)
  * [Deploy](#deploy)

<!-- tocstop -->


### BOSH CLI

今回は[v2 CLI](https://blog.ik.am/entries/404)じゃなくて、古い方のCLIを使う。
念のため、最新化。

``` console
$ sudo gem install bosh_cli --no-ri --no-rdoc
$ bosh -v
BOSH 1.3262.26.0

BOSH Directorが動いていることは前提。

プロジェクト作成

bosh init release nexus-boshrelease --git

.gitignoreも作っておく。

config/dev.yml
config/private.yml
releases/*.tgz
releases/**/*.tgz
dev_releases
.blobs
blobs
.dev_builds
.idea
.DS_Store
.final_builds/jobs/**/*.tgz
.final_builds/packages/**/*.tgz
*.swp
*~
*#
#*
*.tar.gz

Blob作成

JavaとNexusのバイナリをダウンロード

cd nexus-boshrelease
wget https://download.run.pivotal.io/openjdk-jdk/trusty/x86_64/openjdk-1.8.0_121.tar.gz
wget https://sonatype-download.global.ssl.fastly.net/nexus/3/nexus-3.2.1-01-unix.tar.gz

BOSHのblobとして登録

bosh add blob openjdk-1.8.0_121.tar.gz java
bosh add blob nexus-3.2.1-01-unix.tar.gz nexus

blobをblobstore (S3)にアップロード

バケット名などはconfig/final.ymlに。 ``` yml

blobstore: provider: s3 options: bucket_name: nexus-boshrelease region: ap-northeast-1 endpoint: https://s3-ap-northeast-1.amazonaws.com final_name: nexus


> S3を使わない場合は、local blobstoreを使ってもok。1人で同じ開発環境を使っている場合はこれでもok
>
> ``` yml
> ---
> blobstore:
>   provider: local
>   options:
>     blobstore_path: /tmp/nexus
> final_name: nexus
> ```

s3のアクセスキーは`config/private.yml`に。

``` yml
---
blobstore:
  options:
    access_key_id: ....
    secret_access_key: ....

S3のバケットを作成

aws s3 mb s3://nexus-boshrelease

作成したバケットを公開するためのポリシーをpolicy.jsonに記述

{
   "Statement": [
      {
         "Effect": "Allow",
         "Principal": "*",
         "Action": "s3:GetObject",
         "Resource": "arn:aws:s3:::nexus-boshrelease/*"
      }
   ]
}

ポリシーを適用

aws s3api put-bucket-policy --bucket nexus-boshrelease --policy file://policy.json 

blobをblobstoreにアップロード(localの場合不要)

bosh -n upload blobs 

Pacakgeの作成

blobをどう配置するか

Java

bosh generate package java

package/java/packagingに次を記載

set -e

tar xfv java/openjdk-1.8.0_121.tar.gz
cp -a ./* $BOSH_INSTALL_TARGET
rm -rf $BOSH_INSTALL_TARGET/java
rm -rf $BOSH_INSTALL_TARGET/demo
rm -rf $BOSH_INSTALL_TARGET/sample
rm -rf $BOSH_INSTALL_TARGET/man
rm -rf $BOSH_INSTALL_TARGET/src.zip

package/java/specに次を記載

---
name: java

dependencies: []

files:
- java/openjdk-1.8.0_121.tar.gz

Nexus

bosh generate package nexus

package/nexus/packagingに次を記載

set -e

tar xfv nexus/nexus-3.2.1-01-unix.tar.gz
cp -ar ./nexus-3.2.1-01 $BOSH_INSTALL_TARGET/nexus
cp -ar ./sonatype-work $BOSH_INSTALL_TARGET/

package/nexus/specに次を記載

---
name: nexus

dependencies: []

files:
- nexus/nexus-3.2.1-01-unix.tar.gz

Job

bosh generate job nexus

jobs/nexus/templates/ctl.erbに次を記載

#!/bin/bash

JOB_NAME=nexus
RUN_DIR=/var/vcap/sys/run/$JOB_NAME
LOG_DIR=/var/vcap/sys/log/$JOB_NAME
PACKAGE_DIR=/var/vcap/packages/$JOB_NAME
PIDFILE=${RUN_DIR}/pid
JAVA_HOME=/var/vcap/packages/java
PATH=$PATH:$JAVA_HOME/bin

case $1 in

  start)
    mkdir -p $RUN_DIR $LOG_DIR
    chown -R vcap:vcap $RUN_DIR $LOG_DIR

    $PACKAGE_DIR/nexus/bin/nexus start
    cat $PACKAGE_DIR/sonatype-work/nexus3/lock | tr '@' ' ' | awk '{print $1}' > $PIDFILE
    ;;

  stop)
    $PACKAGE_DIR/nexus/bin/nexus stop
    rm -f $PIDFILE
    ;;

  *)
    echo "Usage: ctl {start|stop}" ;;

esac

jobs/nexus/monitに次を記載

check process nexus
 with pidfile /var/vcap/sys/run/nexus/pid
 start program "/var/vcap/jobs/nexus/bin/ctl start"
 stop program "/var/vcap/jobs/nexus/bin/ctl stop"
 group vcap

jobs/nexus/specに次を記載

---
name: nexus
templates:
 ctl.erb: bin/ctl
packages:
- java
- nexus
properties: {}

BOSH Releaseを作成して、Directorにアップロード

bosh create release --name nexus --force
bosh upload release

Deploy

manifest.ymlに次を記載

---
name: nexus

director_uuid: <%= `bosh status --uuid` %>

releases:
- name: nexus
  version: latest

stemcells:
- alias: trusty
  os: ubuntu-trusty
  version: latest

instance_groups:
- name: nexus
  instances: 1
  vm_type: default
  persist_disk: default
  stemcell: trusty
  azs: [az1]
  networks:
  - name: default
  jobs:
  - name: nexus
    release: nexus

update:
  canaries: 1
  max_in_flight: 1
  serial: false
  canary_watch_time: 1000-60000
  update_watch_time: 1000-60000

デプロイ

bosh deployment manifest.yml
bosh deploy

VMをチェック

$ bosh vms
...

VMs total: 1
Deployment 'nexus'

Director task 290872

Task 290872 done

+------------------------------------------------+---------+-----+---------+---------------+
| VM                                             | State   | AZ  | VM Type | IPs           |
+------------------------------------------------+---------+-----+---------+---------------+
| nexus/0 (2063a7c0-2c5a-4890-8cba-0cabc868f6f7) | running | az1 | medium  | 10.244.15.159 |
+------------------------------------------------+---------+-----+---------+---------------+

でNexusのIPを確認

$ curl -I 10.244.15.159:8081
HTTP/1.1 200 OK
Date: Mon, 06 Mar 2017 18:23:44 GMT
Server: Nexus/3.2.1-01 (OSS)
X-Frame-Options: SAMEORIGIN
X-Content-Type-Options: nosniff
Content-Type: text/html
Last-Modified: Mon, 06 Mar 2017 18:23:45 GMT
Pragma: no-cache
Cache-Control: post-check=0, pre-check=0
Expires: 0
Content-Length: 4835

動いた。


✒️️ Edit  ⏰ History  🗑 Delete