GitBucketサーバー構築用Shell-ScriptをVagrant+VirtualBox on Windows10 Home 環境 で実行してみる

## はじめに

粗削りですが、一発インストール用のShell-Scriptを作成したのでその紹介です。

=> [GitHub](https://github.com/mao172/gb-installer) で公開

### 構築するGitBucketサーバーの環境

| Software | Version |
|---|:---:|
|CentOS7| 7.5.1804 |
|PostgreSQL|10.5|
|Java8(OpenJDK) |1.8.0.181 |
|Tomcat 8.5| 8.5.32 / 8.5.33|
|Apache2|2.4.6|
|GitBucket | 4.27.0|

※ 2018/08/14時点

※ 2018/08/17にTomcat v8.5.33がリリースされましたので、最新版に対応できるように修正しました。(8/25追記)

## 準備

「[Win10にVirtualBox+Vagrantを用いた環境構築する為の下準備](https://qiita.com/hein946/items/cc44aa8d4061eb00dc5d)」を参考に、最新の[VirtualBox](http://www.oracle.com/technetwork/server-storage/virtualbox/downloads/index.html)と[Vagrant](https://www.vagrantup.com/downloads.html)（64bit）をインストールする

「[Git for Windowsのインストール手順](https://gabekore.org/git-for-windows-install)」を参考に、Git Bash でSSHが使えるようにする。

| ツール | バージョン |
|---|:---:|
|VirtualBox | 5.2.16 |
|Vagrant | 2.1.2 |
|Git for Windows | 2.18.0 |


## ビルドアップ

1. 適当な作業用のディレクトリを作成する
2. Git Bashを起動し、作成したディレクトリをカレントにする
3. `vagrant init`コマンドを実行する
4. 生成されたVagrantファイルを次のように編集し、`vagrant up`する

```ruby:Vagrantfile

# -*- mode: ruby -*-
# vi: set ft=ruby :

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
  # The most common configuration options are documented and commented below.
  # For a complete reference, please see the online documentation at
  # https://docs.vagrantup.com.

  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://vagrantcloud.com/search.
  config.vm.box = "centos/7"

  # Disable automatic box update checking. If you disable this, then
  # boxes will only be checked for updates when the user runs
  # `vagrant box outdated`. This is not recommended.
  # config.vm.box_check_update = false

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine. In the example below,
  # accessing "localhost:8080" will access port 80 on the guest machine.
  # NOTE: This will enable public access to the opened port
  # config.vm.network "forwarded_port", guest: 80, host: 8080

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine and only allow access
  # via 127.0.0.1 to disable public access
  # config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"

  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
  config.vm.network "private_network", ip: "192.168.33.10"

  # Create a public network, which generally matched to bridged network.
  # Bridged networks make the machine appear as another physical device on
  # your network.
  # config.vm.network "public_network"

  # Share an additional folder to the guest VM. The first argument is
  # the path on the host to the actual folder. The second argument is
  # the path on the guest to mount the folder. And the optional third
  # argument is a set of non-required options.
  # config.vm.synced_folder "../", "/vagrant_data"

  # Provider-specific configuration so you can fine-tune various
  # backing providers for Vagrant. These expose provider-specific options.
  # Example for VirtualBox:
  #
  config.vm.provider "virtualbox" do |vb|
  #   # Display the VirtualBox GUI when booting the machine
  #   vb.gui = true
  #
  #   # Customize the amount of memory on the VM:
    vb.memory = "1024"
  end
  #
  # View the documentation for the provider you are using for more
  # information on available options.

  # Enable provisioning with a shell script. Additional provisioners such as
  # Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
  # documentation for more information about their specific syntax and use.
  # config.vm.provision "shell", inline: <<-SHELL
  #   apt-get update
  #   apt-get install -y apache2
  # SHELL
  config.vm.provision "shell", inline: <<-SHELL
  
    yum reinstall -y glibc-common
    localectl set-locale LANG=ja_JP.UTF-8
    source /etc/locale.conf
    
    timedatectl set-timezone Asia/Tokyo
  
    curl -L https://raw.githubusercontent.com/mao172/gb-installer/master/install.sh | bash
  SHELL
end

```

## 起動確認

ブラウザから、`http://192.168.33.10/gitbucket`にアクセスし、GitBucketにrootユーザーでログインできればOK！

ポートフォワーディングしたい場合は、`config.vm.network "forwarded_port", guest: 80, host: 8080` の行のコメントアウトを外して、`vagrant up`しましょう。

## 以下、おまけ・・・

記事を書き始めた当初は、ローカルに置いたファイルをvagrantから参照して実行するスタイルで作り始めたが、最終的に`curl`コマンドで取得したファイルをbashに渡して実行できるようにしてみた。

ローカル実行用のVagrantfileは[ココ](https://github.com/mao172/gb-installer/blob/master/centos7/Vagrantfile)にあるので、必要であれば、`git clone`でもして参照していただければと思います。

### ローカルディレクトリがマウント出来ない場合（その１）

[参考](http://koukitips.net/post1850/)

#### メッセージ内容

```bash
Vagrant is currently configured to create VirtualBox synced folders with
the `SharedFoldersEnableSymlinksCreate` option enabled. If the Vagrant
guest is not trusted, you may want to disable this option. For more
information on this option, please refer to the VirtualBox manual:

  https://www.virtualbox.org/manual/ch04.html#sharedfolders

This option can be disabled globally with an environment variable:

  VAGRANT_DISABLE_VBOXSYMLINKCREATE=1

or on a per folder basis within the Vagrantfile:

  config.vm.synced_folder '/host/path', '/guest/path', SharedFoldersEnableSymlinksCreate: false

```
#### 対策

```bash
$ vagrant plugin expunge --reinstall

This command permanently deletes all currently installed user plugins. It
should only be used when a repair command is unable to properly fix the
system.

Continue? [N]: y

All user installed plugins have been removed from this Vagrant environment!

Vagrant will now attempt to reinstall user plugins that were removed.

```

### ローカルディレクトリがマウント出来ない場合（その２）

[参考](https://qiita.com/reflet/items/98fd548a6ef341f685bb)

#### メッセージ内容

```bash
Vagrant was unable to mount VirtualBox shared folders. This is usually
because the filesystem "vboxsf" is not available. This filesystem is
made available via the VirtualBox Guest Additions and kernel module.
Please verify that these guest additions are properly installed in the
guest. This is not a bug in Vagrant and is usually caused by a faulty
Vagrant box. For context, the command attempted was:

mount -t vboxsf -o uid=1000,gid=1000 vagrant_data /vagrant_data

The error output from the command was:

mount: unknown filesystem type 'vboxsf'

```

#### 対策

```bash
$ vagrant plugin install vagrant-vbguest
Installing the 'vagrant-vbguest' plugin. This can take a few minutes...
Installed the plugin 'vagrant-vbguest (0.15.2)'!

$ vagrant reload
```