プログラマーの調べ物

プログラマーが調べ物をするときに役に立つサイトを作ります。

Ansibleのpingを使って接続確認をしたら "to use the 'ssh' connection type with passwords, you must install the sshpass program"エラーが出た。

以下のホスト情報で接続を確認。

$ cat hosts
vm-machine ansible_host=192.168.62.130 ansible_port=22 ansible_user=root ansible_ssh_pass=root_pass

以下のコマンド実行。

ansible all -i hosts -m ping

すると、"to use the 'ssh' connection type with passwords, you must install the sshpass program"というエラーが。

vm-machine | FAILED! => {
    "failed": true,
    "msg": "to use the 'ssh' connection type with passwords, you must install the sshpass program"
}

https://stackoverflow.com/questions/42835626/to-use-the-ssh-connection-type-with-passwords-you-must-install-the-sshpass-pr

こいつを解消するには、

# yum install sshpass

でsshpassをインストールしておく必要がある。 インストールしたら接続確認が取れた。

$ ansible all -i hosts -m ping
vm-machine | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

vagrant upを実行したら"VirtualBox is complaining that the kernel module is not loaded."というエラーが出た

vagrant upでエラーが出た。

VirtualBox is complaining that the kernel module is not loaded. Please
run `VBoxManage --version` or open the VirtualBox GUI to see the error
message which should contain instructions on how to fix this error.

指示通りにrootで

VBoxManage --version

を実行。 すると、以下のようなメッセージが出現した。

WARNING: The vboxdrv kernel module is not loaded. Either there is no module
         available for the current kernel (3.10.0-514.el7.x86_64) or it failed to
         load. Please recompile the kernel module and install it by

           sudo /sbin/vboxconfig

         You will not be able to start VMs until this problem is fixed.
5.1.22r115126
/sbin/vboxconfig

を実行すると、以下のようなモジュールをインストールせい、と怒られた。

This system is not currently set up to build kernel modules (system extensions).
Running the following commands should set the system up correctly:

  yum install kernel-devel-3.10.0-514.el7.x86_64
(The last command may fail if your system is not fully updated.)
  yum install kernel-devel
vboxdrv.sh: failed: Look at /var/log/vbox-install.log to find out what went wrong.
This system is not currently set up to build kernel modules (system extensions).
Running the following commands should set the system up correctly:

  yum install kernel-devel-3.10.0-514.el7.x86_64
(The last command may fail if your system is not fully updated.)
  yum install kernel-devel

There were problems setting up VirtualBox.  To re-start the set-up process, run
  /sbin/vboxconfig
as root.

yumでインストール。

# yum install kernel-devel-3.10.0-514.el7.x86_64

インストール後にもう一度vboxconfigを実行すると、リビルドしてくれたっぽい。

# /sbin/vboxconfig
vboxdrv.sh: Stopping VirtualBox services.
vboxdrv.sh: Building VirtualBox kernel modules.
vboxdrv.sh: Starting VirtualBox services.

次にvagrant upすると別のエラーが出た。

No usable default provider could be found for your system.

Vagrant relies on interactions with 3rd party systems, known as
"providers", to provide Vagrant with resources to run development
environments. Examples are VirtualBox, VMware, Hyper-V.

The easiest solution to this message is to install VirtualBox, which
is available for free on all major platforms.

If you believe you already have a provider available, make sure it
is properly installed and configured. You can see more details about
why a particular provider isn't working by forcing usage with
`vagrant up --provider=PROVIDER`, which should give you a more specific
error message for that particular provider.

とりあえずVirtualBoxをremoveして、バージョンを下げて入れ直してみる。

# yum remove VirtualBox-5.1
# yum update
# yum install VirtualBox-4.3

やっとvagrant upが通った…。

CentOS7にVagrantをインストール

まずはVirtualBoxをインストー

標準リポジトリにはVirtualBoxは含まれていないため、VirtualBoxコミュニティが公式に提供しているリポジトリからインストールする。

# yum install epel-release

# cd /etc/yum.repos.d
# wget http://download.virtualbox.org/virtualbox/rpm/rhel/virtualbox.repo
# yum --enablerepo=epel install dkms
# yum -y groupinstall "Development Tools"
# yum -y install kernel-devel
# yum -y install VirtualBox-5.1

DKMSはLinuxカーネルがアップデートされた場合にカーネルモジュールをリビルドして実行してくれるフレームワーク

Vagrantをインストー

以下のコマンドでvagrantをインストー

# yum install https://releases.hashicorp.com/vagrant/1.8.1/vagrant_1.8.1_x86_64.rpm

無事にインストール完了。

# vagrant -v
Vagrant 1.8.1

pipでAnsibleをインストールしたときにエラーが出た

Ansibleをインストー

以下のコマンドでインストールする。

# pip2 install ansible

すると、以下のようなエラーが発生。

    unable to execute gcc: No such file or directory
    error: command 'gcc' failed with exit status 1

    ----------------------------------------
Command "/usr/bin/python -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-7PfuiO/cryptography/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-4PCVVH-record/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-build-7PfuiO/cryptography/
[root@localhost install]#

ははあん、おそらく、DevelopmentKit敵なものが足りないんだな。 Rubyでよくあるやつだ。

以下のコマンドを実行

# yum install -y libffi-devel openssl-devel

# yum -y install python-devel python-crypto

これでいけるか???

p-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -I/usr/include/python2.7 -c build/temp.linux-x86_64-2.7/_openssl.c -o build/temp.linux-x86_64-2.7/build/temp.linux-x86_64-2.7/_openssl.o
    unable to execute gcc: No such file or directory
    error: command 'gcc' failed with exit status 1

    ----------------------------------------
Command "/usr/bin/python -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-osagHo/cryptography/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-_ikNDj-record/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-build-osagHo/cryptography/

だ、ダメだ…! ググろう。

以下のStackoverflowに従って、

https://stackoverflow.com/questions/11094718/error-command-gcc-failed-with-exit-status-1-while-installing-eventlet

# python -V
Python 2.7.5

# yum install python-devel

# yum install libevent-devel

# easy_install gevent

とやってもダメ。

よくよくエラーを見てみると、どうやらgccでエラーが出ている模様。 なので、gccをインストール。

# yum install gcc

すると、

# pip2 install ansible

Building wheels for collected packages: cryptography
  Running setup.py bdist_wheel for cryptography ... done
  Stored in directory: /root/.cache/pip/wheels/ff/a5/ef/186bb4f6a89ef0bb8373bf53e5c9884b96722f0857bd3111b8
Successfully built cryptography
Installing collected packages: cryptography, bcrypt, pyasn1, pynacl, paramiko, ansible
Successfully installed ansible-2.3.1.0 bcrypt-3.1.3 cryptography-1.9 paramiko-2.2.1 pyasn1-0.2.3 pynacl-1.1.2

# ansible --version
ansible 2.3.1.0
  config file =
  configured module search path = Default w/o overrides
  python version = 2.7.5 (default, Nov  6 2016, 00:28:07) [GCC 4.8.5 20150623 (Red Hat 4.8.5-11)]

どうやらインストールがうまくいったようだ。 pipでansibleインストールする際にエラーが出る場合は、必要なパッケージが足りているかどうかを調べてみることが大事。

CentOS7にpipをインストールする

python-devel を入れる

yum -y install python-devel

pip を入れる

packageをアップデートする

yum -y update

python-pip をインストールする

yum -y install python-pip

で、いけるはずが、エラー。

[root@localhost ~]# yum -y install python-pip
読み込んだプラグイン:fastestmirror
Loading mirror speeds from cached hostfile
 * base: ftp.iij.ad.jp
 * extras: ftp.iij.ad.jp
 * updates: ftp.iij.ad.jp
パッケージ python-pip は利用できません。

yumを使ってpipをインストールするには以下のようにEPELリポジトリをインストールする必要がある模様。

wget http://ftp-srv2.kddilabs.jp/Linux/distributions/fedora/epel/beta/7/x86_64/epel-release-7-0.2.noarch.rpm

sudo rpm -ivh epel-release-7-0.2.noarch.rpm

sudo yum install -y python-pip

http://cordea.hatenadiary.com/entry/2014/07/21/203742

これが面倒な場合は、以下の方法を使おう。

pipをcurlpythonでインストールする

# curl "https://bootstrap.pypa.io/get-pip.py" -o "get-pip.py"

# python get-pip.py
# pip -V
pip 9.0.1 from /usr/lib/python2.7/site-packages (python 2.7)

ElectronでHello Worldアプリを作成する

ElectronはGitHubによって開発されたデスクトップアプリケーションフレームワークです。 AtomやSlackのクライアントアプリもElectronを使って作られています。

electronのhello world appを作ってみましょう。

まずはnpm install -g electronでelectronをインストールします。

今回のプロジェクトのフォルダ構成は以下のとおりです。

f:id:sho322:20170704203736j:plain

適当なフォルダにpackage.jsonを作りましょう。

{
    "name": "hello-world-nwjs",
    "main": "index.html",
    "version": "1.0,0"
}

次に、main.jsを作ります。

'use strict';

const electron = require('electron');
const app = electron.app;
const BrowserWindow = electron.BrowserWindow;

let mainWindow = null;

app.on('window-all-closed', () => {
    if (process.platform !== 'darwin') app.quit();
});

app.on('ready', () => {
    mainWindow = new BrowserWindow();
    mainWindow.loadURL(`file://${__dirname}/index.html`);
    mainWindow.on('closed', () => {mainWindow = null;});
})

最後に、main.jsが読み込むindex.htmlを作成します。

<html>
  <head>
   <title>Hello Electron</title>
   <style>
        body {
            background-image: linear-gradient (45deg, #EAD790 0%, #EF8C53 100%);
            text-align: center;
        }

        button {
            background: rgba(0,0,0,0.40);
            box-shadow: 0px 0px 4px 0px rgba(0,0,0,0.50);
            border-radius: 8px;
            color: white;
            padding: 1em 2em;
            border: none;
            font-family: 'Roboto', sans-serif;
            font-weight: 100;
            font-size: 14pt;
            position: relative;
            top: 40%;
            cursor: pointer;
            outline:none;         
        }
    </style>
   <link href='https://fonts.googleapis.com/css?family=Roboto' rel='stylesheet' type='text/css'>
   <script>
       function sayHello() {
           alert('Hello World');
       }
   </script>
  </head>
  <body>
      <button onclick="sayHello()">Say Hello!</button>
  </body>
</html>

早速実行してみましょう。 electron .

で実行すると、以下のような画面が表示されます。

f:id:sho322:20170704203749j:plain

WidowsにDockerをインストールする

Docker Toolboxのダウンロードとインストール

  • 以下からインストールhttps://www.docker.com/products/docker-toolbox
  • インストール後、「Docker Quickstart Terminal」を実行
  • Docker ToolboxがDockerを動作させるための仮想環境を作成
  • デスクトップのOracle VirtualBoxをダブルクリック   * defaultという名前の仮想環境が実行中となる

  • docker-machine ssh defaultでコンソールから仮想環境に接続できる

Dockerで Hello World

  • docker run ubuntu:latest /bin/echo 'Hello world'
  • ローカル環境にないイメージはDockerリポジトリからDockerイメージをダウンロードできる

Docker KitematicでGUI操作

Dockerの操作

  • CentOS7をダウンロードできる
    • docker pull centos:7
  • 取得したイメージの一覧を表示
    • docker images
  • イメージの詳細確認
    • docker inspect [imageID]
  • イメージにタグを設定する
    • docker tag centos:7 myname/basicserver:0.1
  • イメージの検索
    • docker search keyword
  • イメージの削除
    • docker rmi [image name]