かべぎわブログ

ブログです

waitで別プロセスの終了を待ち合わせてから実行する

概要

シェルスクリプト中で別プロセスでコマンドを実行して、その終了を待ち合わせてから先に進むみたいな並列実行を実現してみます。

スクリプト

こんなかんじ。
waitコマンドは指定したプロセスIDを待ち合わせることができます。
それを利用します。

#!/bin/bash

command1() {
  sleep 1
  echo 'command1'
  date
} 
command2() {
  sleep 2
  echo 'command2'
  date
} 
command3() {
  sleep 3
  echo 'command3'
  date
} 

command1 &
pid1=${!}
command2 &
pid2=${!}
command3 &
pid3=${!}

wait ${pid1} ${pid2} ${pid3}
echo 'done'

実行してみる

実行するとこんなかんじ。
同時に動いていそうですね。

$ ./wait.sh 
command1
Sun Mar  8 09:00:29 UTC 2020
command2
Sun Mar  8 09:00:30 UTC 2020
command3
Sun Mar  8 09:00:31 UTC 2020
done

おわりに

つかっていきたいですね。

シェルスクリプトで連想配列をつかう

概要

シェルスクリプトで連想配列をつかってみます。

スクリプト

こんなかんじ。
declare -Aで宣言してあげます。

#!/bin/bash

declare -A test_array

test_array['name']='kabegiwa'
test_array['age']=28

echo "name: ${test_array[name]}"
echo "age: ${test_array[age]}"

実行してあげるとこんなかんじ。

$ ./associative_array.sh 
name: kabegiwa
age: 28

おわりに

べんりですね

新しいLinuxの教科書

新しいLinuxの教科書

CircleCIのローカルCLIをUbuntuにインストールする

概要

UbuntuにCircleCIのローカルCLIをインストールしてみます。

前提

$ cat /etc/os-release | grep PRETTY_NAME
PRETTY_NAME="Ubuntu 18.04.4 LTS"

手順

snapのインストール

sudo apt install snapd

CircleCIのローカルCLIのインストール

sudo snap install circleci

確認

$ circleci version
0.1.6648+8e35edb

おわりに

インストールできましたね。

Ubuntuスタートアップバイブル

Ubuntuスタートアップバイブル

  • 作者:小林 準
  • 発売日: 2018/07/30
  • メディア: 大型本

Slay the Spire 全キャラで心臓倒した

Slay the Spireを全キャラクターで心臓を倒した。
アセンションはレベル0
50時間くらい溶けた。

アイアンクラッド

バリケードと不動を積んで殴る
f:id:kabegiwakun:20200224171344j:plain

サイレント

毒入れて弱体化させてナイフなげる
f:id:kabegiwakun:20200224171503j:plain

ディフェクト

フロストでディフェンス固めてちまちま殴る
f:id:kabegiwakun:20200224171537j:plain

ウォッチャー

平静で防御を固めてマントラ貯める f:id:kabegiwakun:20200224171641j:plain

Slay the Spire|オンラインコード版

Slay the Spire|オンラインコード版

  • 発売日: 2019/12/02
  • メディア: Software Download

AWSの月間利用料を取得するPythonを書いた

概要

PythonでAWSの月間利用料を取得するスクリプトをかきました。
今回のものは他のPythonスクリプトから読み込ませることも意識しています。

成果物

これです。 github.com

usage

Command line

python get_monthly_cost.py --help
usage: get_monthly_cost.py [-h] [--profile [PROFILE]] [--year [YEAR]]
                           [--month [MONTH]]

optional arguments:
  -h, --help           show this help message and exit
  --profile [PROFILE]  Use a specific profile from your credential file.
  --year [YEAR]        Sets the year for retrieving AWS costs.
  --month [MONTH]      Sets the month for retrieving AWS costs.
$ python get_monthly_cost.py
9.4029780888 USD
$ python get_monthly_cost.py --year 2020 --month 1
10.2359175577 USD

Programming

import get_monthly_cost
response = get_monthly_cost.cost().get()
# ('9.4029780888', 'USD')
import get_monthly_cost
response = get_monthly_cost.cost(2020,1).get()
# ('10.2359175577', 'USD')

図解即戦力 Amazon Web Servicesのしくみと技術がこれ1冊でしっかりわかる教科書

図解即戦力 Amazon Web Servicesのしくみと技術がこれ1冊でしっかりわかる教科書

  • 作者:小笠原 種高
  • 出版社/メーカー: 技術評論社
  • 発売日: 2019/11/07
  • メディア: 単行本(ソフトカバー)

CircleCIでpersist_to,attach_workspaceをつかってJob間でファイルを共有する

概要

CircleCIでpersist_to_workspaceattach_workspaceを利用してJob間でファイルを共有してみます。

たとえばビルドした結果を別のJobでデプロイなどよくありがちだと思う。

.circleci/config.yaml

こんなかんじ。
今回はテスト用にechoをリダイレクトするJobとそれを確認するJobでわけてみた。

version: 2.1

jobs:
  build:
    docker:
      - image: circleci/python:3.7
    working_directory: ~/repo
    steps:
    - run:
        command: |
          mkdir build
          echo 'wawawa' >> build/wawawa.txt

    - persist_to_workspace:
        root: ~/repo
        paths:
            - build/* 
  deploy:
    docker:
      - image: circleci/python:3.7
    working_directory: ~/repo
    steps:
    - attach_workspace:
        at: ~/repo
    - run:
        command: |
          pwd
          ls -l 
          ls -l build 

workflows:
   main:
     jobs:
     - build 
     - deploy:
         requires:
           - build

結果

できた。

おわりに

おくがふかいですね

インフラCI実践ガイド Ansible/GitLabを使ったインフラ改善サイクルの実現

インフラCI実践ガイド Ansible/GitLabを使ったインフラ改善サイクルの実現

Kubernetesのpodのラベルでフィルタリングする

概要

KubernetesのPodのラベルでフィルタリングして条件に一致するリソースのみ出力してみたいと思います。

準備

こんなかんじでpodをつくってます。
label-test.yaml

---
apiVersion: v1
kind: Pod
metadata:
  name: sample-label
  labels:
    label1: wawawa
    label2: sasasa
spec:
  containers:
    - name: test-pod1
      image: nginx
---
apiVersion: v1
kind: Pod
metadata:
  name: sample-label2
  labels:
    label1: wawawa
    label3: dadada 
spec:
  containers:
    - name: test-pod2
      image: nginx
$ kubectl get pods
NAME            READY   STATUS    RESTARTS   AGE
sample-label    1/1     Running   0          40s
sample-label2   1/1     Running   0          40s

ラベルでしぼりこむ

label2がsasasaであるpodのみ出力してみます。

$ kubectl get pods -l label2=sasasa
NAME           READY   STATUS    RESTARTS   AGE
sample-label   1/1     Running   0          3m58s

-Lオプションをつけるとラベル名も表示してくれます。

$ kubectl get pods -l label2=sasasa -L label2
NAME           READY   STATUS    RESTARTS   AGE     LABEL2
sample-label   1/1     Running   0          4m28s   sasasa

おわりに

べんりですね

Kubernetesで実践するクラウドネイティブDevOps

Kubernetesで実践するクラウドネイティブDevOps

  • 作者:John Arundel,Justin Domingus
  • 出版社/メーカー: オライリージャパン
  • 発売日: 2020/02/19
  • メディア: 単行本(ソフトカバー)

VagrantでCPU数とメモリサイズを設定する

概要

VagrantでCPUの数とメモリのサイズを設定してみます。

Vagrantfile

Vagrantfileに以下のように設定します。

  config.vm.provider "virtualbox" do |v|
    v.memory = 8192
    v.cpus = 4
  end

おわりに

できましたね。

$  free
              total        used        free      shared  buff/cache   available
Mem:        8166932      684508     5470900        8352     2011524     7240052
Swap:       1003516           0     1003516
$ lscpu | grep CPU
CPU op-mode(s):      32-bit, 64-bit
CPU(s):              4

実践 Vagrant

実践 Vagrant

  • 作者:Mitchell Hashimoto
  • 出版社/メーカー: オライリージャパン
  • 発売日: 2014/02/21
  • メディア: 単行本(ソフトカバー)

Ubuntu18にMinikubeをインストールする

概要

Ubuntu18にMinikubeをインストールしてみます。

インストール手順

手順です。

システムを最新バージョンに更新する

sudo apt-get update -y
sudo apt-get upgrade -y

インストールに必要なパッケージのインストール

sudo apt-get install curl wget apt-transport-https -y

VirtualBox Hypervisorのインストール

途中で簡易GUIがでてくるので支持に従ってインストールします。

apt-get install virtualbox virtualbox-ext-pack

Minikubeのインストール

公式WebサイトからMinikubeをダウンロードします。

wget https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64

ダウンロードしたファイルを/usr/local/binにコピー

sudo cp minikube-linux-amd64 /usr/local/bin/minikube

minikubeに実行権限を付与

sudo chmod 755 /usr/local/bin/minikube

インストールできました

minikube version

minikube version: v1.7.2
commit: 50d543b5fcb0e1c0d7c27b1398a9a9790df09dfb

おわりに

ローカルで開発していく。

入門 Kubernetes

入門 Kubernetes

VagrantでUbuntu18を起動してRLoginでssh接続する

概要

VagrantでUbuntu18を起動してRLoginでssh接続してみます。

Vagrantの準備

ローカルの適当なディレクトリで以下コマンドを実行します。

$ vagrant init bento/ubuntu-18.04
$ vagrant up

RLoginの準備

こんなかんじにします。
f:id:kabegiwakun:20200215154230p:plain

SSH認証鍵のところは以下を設定します。

C:\Users\ユーザ名\vagrant\ubuntu18.vagrant\machines\default\virtualbox\private_key

接続

接続できました。

vagrant@vagrant:~$ uname -a
Linux vagrant 4.15.0-76-generic #86-Ubuntu SMP Fri Jan 17 17:24:28 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux

おわりに

べんりですね。

Ubuntu はじめる&楽しむ 100%活用ガイド[Ubuntu 18.04LTS 日本語Remix対応]

Ubuntu はじめる&楽しむ 100%活用ガイド[Ubuntu 18.04LTS 日本語Remix対応]

  • 作者:リンクアップ
  • 出版社/メーカー: 技術評論社
  • 発売日: 2018/09/01
  • メディア: 単行本(ソフトカバー)