Vagrantで複数ゲストOSを立ててみる
やりたいこと
Vagrant
で複数ゲストOSを作って更にPostgreSQLをインストールしたい。
VagrantのVersion
以下の通りです。
➜ ~ vagrant -v Vagrant 2.2.0
Vagrantfileを作ってみる
以下のようにしました。
ポイントは以下です。
config.vm.define
のところを作成するゲストOS分定義します- 今回は3つのゲストOSを定義し、それぞれdb1, db2, db3と割りふっています
- 今回はCentOS7なので、
config.vm.box = "centos/7"
で共通的に定義します - sshで繋ぐことを考慮して、ゲストOSごとにIPアドレスを分けます
- ゲストOS作成後のスクリプトとして、以下2つを定義し、
xxx.vm.provision
で読みこませます- yumのアップデートを行う
$yum_script
- PostgreSQLのインストール、初期セットアップを行う
$postgresql_script
- yumのアップデートを行う
# -*- mode: ruby -*- # vi: set ft=ruby : $yum_script = <<EOF yum update -y EOF $postgresql_script = <<EOF yum -y localinstall https://download.postgresql.org/pub/repos/yum/10/redhat/rhel-7-x86_64/pgdg-centos10-10-2.noarch.rpm yum -y install postgresql10-server /usr/pgsql-10/bin/postgres --version /usr/pgsql-10/bin/postgresql-10-setup initdb EOF Vagrant.configure("2") do |config| config.vm.box = "centos/7" config.vm.define :db1 do |db1| db1.vm.network "private_network", ip: "192.168.33.10" db1.vm.provision "shell", inline: $yum_script db1.vm.provision "shell", inline: $postgresql_script end config.vm.define :db2 do |db2| db2.vm.network "private_network", ip: "192.168.33.11" db2.vm.provision "shell", inline: $yum_script db2.vm.provision "shell", inline: $postgresql_script end config.vm.define :db3 do |db3| db3.vm.network "private_network", ip: "192.168.33.12" db3.vm.provision "shell", inline: $yum_script db3.vm.provision "shell", inline: $postgresql_script end end
vagrant up
うまく出来ました。yumアップデート、postgresqlインストールも出来ていい感じです。
あとはスクリプトをもうちょっと整理したりすると良さそう。
ここまでくるとChefとかAnsible使ったほうがいいのではないかなと思うけれども。
はじめてのVagrant
やりたいこと
Vagrant
を使うとゲストOSを容易に作れるというのを聞いたので、ちょっとやってみようと。
Vagrantインストール
https://www.vagrantup.com/にアクセスし、[Download 2.2.0]リンクを押下するとダウンロードページに飛びます。
各OSごとのインストーラへのリンクがあるので、macOSのを踏みます。
あとは適当に。
インストールしたVagrantのVersion
以下の通りです。
➜ ~ vagrant -v Vagrant 2.2.0
VagrantでCentOSのVMを作ってみる
実行したスクリプトを以下に載せます。
# 作業用ディレクトリ作成 mkdir -p ~/vagrant/centos7 cd ~/vagrant/centos7 # 初期化実行 vagrant init
この時点で/vagrant/centos7/の中にVagrantfile
が出来ます。
Vagrantfile
は、VM作成に必要となる処理を記述していく設定ファイルのようなものですね。(なお、rubyで書かれています)
今回は以下のように設定しました。
# -*- 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 "../data", "/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 end
デフォルトの設定と違うところを以下に抜粋して載せます。
config.vm.box = "centos/7" config.vm.network "private_network", ip: "192.168.33.10"
# Vagrantfileに従ってゲストOSを作成、設定するコマンド vagrant up
初回なので、BoxのDLに少々時間がかかりました。
実行ログを以下に載せておきます。
Bringing machine 'default' up with 'virtualbox' provider... ==> default: Box 'centos/7' could not be found. Attempting to find and install... default: Box Provider: virtualbox default: Box Version: >= 0 ==> default: Loading metadata for box 'centos/7' default: URL: https://vagrantcloud.com/centos/7 ==> default: Adding box 'centos/7' (v1809.01) for provider: virtualbox default: Downloading: https://vagrantcloud.com/centos/boxes/7/versions/1809.01/providers/virtualbox.box default: Download redirected to host: cloud.centos.org ==> default: Successfully added box 'centos/7' (v1809.01) for 'virtualbox'! ==> default: Importing base box 'centos/7'... ==> default: Matching MAC address for NAT networking... ==> default: Checking if box 'centos/7' is up to date... ==> default: Setting the name of the VM: centos7_default_1541766043966_84174 ==> default: Clearing any previously set network interfaces... ==> default: Preparing network interfaces based on configuration... default: Adapter 1: nat default: Adapter 2: hostonly ==> default: Forwarding ports... default: 22 (guest) => 2222 (host) (adapter 1) ==> default: Booting VM... ==> default: Waiting for machine to boot. This may take a few minutes... default: SSH address: 127.0.0.1:2222 default: SSH username: vagrant default: SSH auth method: private key default: default: Vagrant insecure key detected. Vagrant will automatically replace default: this with a newly generated keypair for better security. default: default: Inserting generated public key within guest... default: Removing insecure key from the guest if it's present... default: Key inserted! Disconnecting and reconnecting using new SSH key... ==> default: Machine booted and ready! ==> default: Checking for guest additions in VM... default: No guest additions were detected on the base box for this VM! Guest default: additions are required for forwarded ports, shared folders, host only default: networking, and more. If SSH fails on this machine, please install default: the guest additions and repackage the box to continue. default: default: This is not an error message; everything may continue to work properly, default: in which case you may ignore this message. ==> default: Configuring and enabling network interfaces... default: SSH address: 127.0.0.1:2222 default: SSH username: vagrant default: SSH auth method: private key ==> default: Rsyncing folder: /Users/xxx/vagrant/centos7/ => /vagrant
ゲストOSのVMが作成され、起動しています。
この状態でVirtualBox
を見ると、centos7_default_xxxxx
というVMが追加されていることが分かります。
Vagrantでssh接続
この状態でsshするとあっさり繋がります。
vagrant ssh
すんごい簡単ですね…。
VirtualBoxにVM作ってISO読みこませてOSインストールして…という手順が不要になりました。
vagrant destroy
で、作成したゲストマシンを削除することも出来るので、ビルド&スクラップがかなり楽に出来そうです。
VirtualBoxに立てたCentOSにsshで繋ぐ
やりたかったこと
VirtualBoxに立てたCentOS7にホストOSからsshで接続したい。
手順
以下が大変参考になった。
もうリンクに全て書かれているのですが、備忘録としてこちらでやった時のメモを残します。
VirtualBoxの設定
VirtualBoxメニューバー -> ファイル -> ホストネットワークマネージャ 初期状態であるvboxnet0のプロパティで、アダプターを手動で設定にチェックをつけ、 IPv4アドレスを192.168.56.1とする。作成したVMにネットワークを追加
作成済みのVMの設定 -> ネットワーク -> アダプター2にホストオンリーアダプタを設定する。
名前はvboxnet0。centosにrootでログイン
以下コマンドを打つ。
# nmtui
Edit a connectionを選択。
文字化けしているやつ(enp0s3ではないほう)を選択。
Profile nameをenp0s8に変更、IPv4をManual、Addressesを192.168.56.10/24にする。
Ipv6をIgnoreにし、Automatically connectにチェックをつけてQuit.
- ネットワークのサービスを再起動
以下コマンドを打つ。
# service NetworkManager restart # service network restart
- sshで接続
ホスト側で以下コマンドを打つ。
ssh root@192.168.56.10
JavaScriptでAsync-Await-Catch
ちょっと予習。
async function sleep(wait, result) { return new Promise((resolve, reject) => { setTimeout(() => { if (result === 3) { reject('error!') } else { console.log(result) resolve() } }, wait) }) } async function syncSleep() { await sleep(2000, 1) await sleep(100, 2) await sleep(1000, 3) } syncSleep().catch(error => console.log('Error', error))
ポイントとなるのは、
- sleepでPromiseオブジェクトを返すこと
- Promiseの中でreject, resolveで正常/異常を区別つける
- 時間がかかる関数、それを呼び出す関数はasyncをつけること
でしょうかね。
Reactのマニアックな使い方
なかなか面倒そうな要件があり、どうにか出来ないかなと思っていたのですが…
実現方法が思いうかばずStackOverFlowに投稿してもた。心より恥じる。恥じることはないかもだけど。
MacでWindows10 on BootCamp
やんごとなき事情で、Windows 10 on BootCampしたので簡単にメモ。
用意するもの
- USBメモリィ 16GB
- Windows10 ISOイメージ
- Windows10 プロダクトキー
以下のサイトをなぞりながらインストールしていきました。
道中、ハマったことといえば、
1. Windows 10のネットワークアダプタに無線LANが出てこない
2. Windows 10のネットワークが不安定
ぐらいです。
ハマったこと(1)は、以下、 support.apple.com
ハマったこと(2)は、以下、
で、それぞれ解決しました。しばらくは様子見てみます。
Javassistで対象クラスのアノテーション情報を取得
Javassitはいわゆるバイトコード操作ライブラリですが、クラスの解析も出来るんじゃないかなと思いました。
ということで、やってみた記事になります。
解析対象クラス
このクラスのメソッドに付与されているアノテーションを取得してみます。
雑いテストクラスで正直すまんこってす。
package oreore.tool.detector.target; public class Target { Date date; LocalDate localDate; @Deprecated public Date getDate() { return date; } @Transient public void setDate(Date date) { this.date = date; } @Addressing public LocalDate getLocalDate() { return localDate; } public void setLocalDate(LocalDate localDate) { this.localDate = localDate; } }
アノテーションを解析するクラス
ちょっとOOPっぽい形にしてみました。
まず親がこれ。
public abstract class Detector { CtClass loadClass(final String fqcn) { ClassPool classPool = ClassPool.getDefault(); CtClass ctClass = null; try { ctClass = classPool.get(fqcn); } catch (NotFoundException e) { e.printStackTrace(); } return ctClass; } protected abstract DetectorResult analyze(final CtClass ctClass, final DetectorResult detectorResult); public DetectorResult invoke(final String fqcn) { final CtClass ctClass = loadClass(fqcn); final DetectorResult detectorResult = new DetectorResult(); analyze(ctClass, detectorResult); return detectorResult; } public static class DetectorResult { private Map<String, Object> information = new HashMap<>(); public Map<String, Object> getInformation() { return information; } public void addInformation(final String key, final List list) { this.information.put(key, list); } } }
そして、アノテーション解析するクラスです。
こいつの中にpsmvを定義しているのはご愛嬌。(面倒くさかっただけともいう)
public class AnnotationDetector extends Detector { @Override protected DetectorResult analyze(CtClass ctClass, DetectorResult detectorResult) { final Stream<CtMethod> ctMethodStream = Arrays.stream(ctClass.getMethods()); ctMethodStream.forEach(method -> { try { final List<Object> annotations = Arrays.asList(method.getAnnotations()); detectorResult.addInformation(method.getName(), annotations); } catch (ClassNotFoundException e) { e.printStackTrace(); } }); return detectorResult; } public static void main(String[] args) { final Detector annotationDetector = new AnnotationDetector(); final DetectorResult result = annotationDetector.invoke("oreore.tool.detector.target.Target"); for (Map.Entry<String, Object> entry: result.getInformation().entrySet()) { System.out.println(entry.getKey() + " : " + entry.getValue()); } } }
実行結果
ふむ。
getClass : [] wait : [] notifyAll : [] notify : [] getLocalDate : [@javax.xml.ws.soap.Addressing] setLocalDate : [] hashCode : [] equals : [] clone : [] setDate : [@java.beans.Transient] finalize : [] toString : [] getDate : [@java.lang.Deprecated]
まとめ
JavaのReflection APIでも同様の事が出来るらしいので、わざわざJavassistを使う必要はないと思います。 解析結果に基づいて元クラスにごにょごにょするようなユースケースがあればJavassistの出番ではないでしょうか。