100MBを超えるファイルを間違えてコミットしてgit pushできない、、
gitignoreの設定をせずにcommitしていたら、100MBを超えるファイルをコミットしてしまい、 githunにpushができなくなってしまった、、
$ git push Counting objects: 29, done. Delta compression using up to 4 threads. Compressing objects: 100% (24/24), done. Writing objects: 100% (29/29), 36.99 MiB | 1.17 MiB/s, done. Total 29 (delta 2), reused 0 (delta 0) remote: Resolving deltas: 100% (2/2), done. remote: error: GH001: Large files detected. You may want to try Git Large File Storage - https://git-lfs.github.com. remote: error: File example/.terraform/plugins/darwin_amd64/terraform-provider-aws_v2.62.0_x4 is 176.06 MB; this exceeds GitHub's file size limit of 100.00 MB
gitignoreを設定し、以下のコマンドで管理対象から外してもpushすることはできない。
$ git rm --cached example/.terraform/
gitの履歴には残っているのでpush対象から消えるわけではない。 こういう時は、以下のコマンドで履歴から抹消する必要がある。
$ git filter-branch --force --index-filter \ "git rm --cached --ignore-unmatch PATH-TO-YOUR-FILE-WITH-SENSITIVE-DATA" \ --prune-empty --tag-name-filter cat -- --all $ git push Counting objects: 28, done. Delta compression using up to 4 threads. Compressing objects: 100% (22/22), done. Writing objects: 100% (28/28), 4.84 KiB | 261.00 KiB/s, done. Total 28 (delta 2), reused 0 (delta 0) remote: Resolving deltas: 100% (2/2), done. To github.com:sinozu/terraform-practice.git * [new branch] master -> master
できた!
詳しくはgithunのヘルプに。 help.github.com
Terraformの基本的な構文
variable
変数を定義することができる。
variable "example_instancetype" { default = "t3.micro" } resource "aws_instance" "example" { ami = "ami-0c3fd0f5d33134a76" instance_type = var.example_instancetype }
variableで定義した変数は実行する際に上書きすることが可能。
$ terraform plan -var 'example_instancetype=t3.nano'
locals
ローカル変数を定義することができる。 variableと違いコマンド実行時に上書きすることができない。
locals { example_instancetype = "t3.micro" } resource "aws_instance" "example" { ami = "ami-0c3fd0f5d33134a76" instance_type = local.example_instancetype }
データソース
外部データの参照することができる。 以下のように定義すると最新のAmazonLinux2のAMIを参照することが可能。
data "aws_ami" "recent_amazon_linux_2" { most_recent = true owners= ["amazon"] filter { name = "name" values = ["amzn2-ami-hvm-2.0.????????-x86_64-gp2"] } filter { name = "state" values = ["available"] } } resource "aws_instance" "example" { ami = data.aws_ami.recent_amazon_linux_2.image_id instance_type = "t3.micro" }
プロバイダ
AWS、GCPなど各プラットフォームの違いを吸収してくれる。
AWS
provider "aws" { region = "ap-northeast-1" }
GCP
provider "google" { credentials = file("account.json") project = "my-project-id" region = "us-central1" }
その他のプロバイダの設定は以下を参照
Providers - Terraform by HashiCorp
他のリソースの参照
TYPE.NAME.ATTRIBUTE
形式で記載すると、他のリソースの値を参照できる。
resource "aws_security_group" "example_ec2" { name = "example-ec2" ingress { from_port = 80 to_port = 80 protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] } egress { from_port = 0 to_port = 0 protocol = "-1" cidr_blocks = ["0.0.0.0/0"] } } resource "aws_instance" "example" { ami = "ami-0c3fd0f5d33134a76" instance_type = local.example_instancetype # 上で作ったVPCを参照 vpc_security_group_ids = [aws_security_group.example_ec2.id] }
おわり!
MacにTerraformとtfenvをインストールする
Terraformのインストール
brew installするだけ
$brew install terraform # インストールできたらバージョンを確認 $ terraform --version Terraform v0.12.25
tfenvのインストール
tfenvはTerraformのバージョンマネージャ。
tfenvを使うと、Terraformのバージョンを簡単に切り替えることができるようになる。
これもbrew installするだけ、、だと思ったらエラーになった。
$ brew install tfenv Updating Homebrew... Error: Cannot install tfenv because conflicting formulae are installed. terraform: because tfenv symlinks terraform binaries Please `brew unlink terraform` before continuing. Unlinking removes a formula's symlinks from /usr/local. You can link the formula again after the install finishes. You can --force this install, but the build may fail or cause obscure side effects in the resulting software.
すでにシンボリックシンクが貼られているとダメみたいなので、先にunlinkする
$ brew unlink terraform Unlinking /usr/local/Cellar/terraform/0.12.25... 1 symlinks removed $ brew install tfenv Updating Homebrew... ==> Downloading https://github.com/tfutils/tfenv/archive/v1.0.2.tar.gz ==> Downloading from https://codeload.github.com/tfutils/tfenv/tar.gz/v1.0.2 ######################################################################## 100.0% 🍺 /usr/local/Cellar/tfenv/1.0.2: 20 files, 29.2KB, built in 4 seconds # バージョン確認 $ tfenv --version tfenv 1.0.2
インストールできた!
tfenvの使い方
インストールされているバージョンの確認
$ tfenv list-remote 0.12.25 0.12.24 0.12.23 …
特定バージョンのインストール
$ tfenv install 0.12.25 [INFO] Installing Terraform v0.12.25 [INFO] Downloading release tarball from https://releases.hashicorp.com/terraform/0.12.25/terraform_0.12.25_darwin_amd64.zip ######################################################################## 100.0% [INFO] Downloading SHA hash file from https://releases.hashicorp.com/terraform/0.12.25/terraform_0.12.25_SHA256SUMS tfenv: tfenv-install: [WARN] No keybase install found, skipping OpenPGP signature verification Archive: tfenv_download.qmW5u9/terraform_0.12.25_darwin_amd64.zip inflating: /usr/local/Cellar/tfenv/1.0.2/versions/0.12.25/terraform [INFO] Installation of terraform v0.12.25 successful [INFO] Switching to v0.12.25 [INFO] Switching completed
インストール済みのバージョン確認
$ tfenv list * 0.12.25 (set by /usr/local/Cellar/tfenv/1.0.2/version)
Terraformのバージョン切り替え
$ tfenv list * 0.12.25 (set by /usr/local/Cellar/tfenv/1.0.2/version) 0.12.24 $ tfenv use 0.12.24 [INFO] Switching to v0.12.24 [INFO] Switching completed $ tfenv list 0.12.25 * 0.12.24 (set by /usr/local/Cellar/tfenv/1.0.2/version) $ terraform --version Terraform v0.12.24 Your version of Terraform is out of date! The latest version is 0.12.25. You can update by downloading from https://www.terraform.io/downloads.html
めっちゃ便利!
errbotをインストールする
errbotとは
pythonで書かれたチャットボット。 github.com
pythonでSlackボットを作りたいと思ったので使ってみる!
インストール
下記コマンドでインストール
$ pip install errbot $ pip install slackclient
インストールが完了したら、初期化を行う
$ mkdir errbot $ cd errbot/ $ errbot --init
Slackの設定を記載する
$ vim config.py # Slackに変更する BACKEND = 'Slack' BOT_DATA_DIR = r'/home/ec2-user/errbot/data' BOT_EXTRA_PLUGIN_DIR = r'/home/ec2-user/errbot/plugins' BOT_LOG_FILE = r'/home/ec2-user/errbot/errbot.log' BOT_LOG_LEVEL = logging.DEBUG # botの管理ユーザを記載する BOT_ADMINS = ('@CHANGE_ME', ) # Tokenを記載 BOT_IDENTITY = { 'token': 'XXXXXXX' }
tokenは以下からbotを作って取得する
https://my.slack.com/services/new/bot
errbotを起動する
$ errbot
Slackで!tryme
と送って、返事が返ってきたら設定完了!
Amazon Linuxにpyenvをインストールする
必要なライブラリのインストール
$ sudo yum install gcc gcc-c++ make git openssl-devel bzip2-devel zlib-devel readline-devel sqlite-devel
pyenvのダウンロード
$ git clone https://github.com/yyuu/pyenv.git ~/.pyenv
pyenvにパスを通す
$ echo 'export PYENV_ROOT="${HOME}/.pyenv"' >> ~/.bashrc $ echo 'if [ -d "${PYENV_ROOT}" ]; then' >> ~/.bashrc $ echo 'export PATH=${PYENV_ROOT}/bin:$PATH' >> ~/.bashrc $ echo 'eval "$(pyenv init -)"' >> ~/.bashrc $ echo 'fi' >> ~/.bashrc # 読み込み $ source ~/.bashrc
python3.6.5のインストール&バージョン切り替え
$ pyenv install 3.6.5 Downloading Python-3.6.5.tar.xz... -> https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tar.xz Installing Python-3.6.5... Installed Python-3.6.5 to /home/ec2-user/.pyenv/versions/3.6.5 $ pyenv versions * system (set by /home/ec2-user/.pyenv/version) 3.6.5 $ pyenv global 3.6.5 $ python --version Python 3.6.5
おわりっ
Go言語でスクレイピングするためにgoqueryを使ってみる
使うライブラリ
goqueryのインストール
go getコマンドを実行するだけ
go get github.com/PuerkitoBio/goquery
リンクを抽出してみる
package main import ( "fmt" "github.com/PuerkitoBio/goquery" ) func main() { // :=演算子は初期値を宣言する doc, err := goquery.NewDocument("http://example.com") if err != nil { fmt.Printf("Failed") } title := doc.Find("title").Text() // println : 改行付き出力 fmt.Println(title) doc.Find("a").Each(func(i int, s *goquery.Selection) { // Attrの戻り値 val string, exists bool link, _ := s.Attr("href") fmt.Println(link) }) }
あとは実行するだけ!
$ go run scraping.go
すごく手軽にスクレイピングを始めることができた。 簡単なことしかやってないが、いろいろとできそう!
【PHP】objectは参照型
以下のようなコードを書いていた際に問題が起きた。
<?php $hoge = new stdClass(); $hoge->a = 1; $fuga = $hoge; $fuga->a = 2; print_r($hoge);
$hoge->a
は1
だと思っていたのに、出力は以下のようになった。
stdClass Object ( [a] => 2 )
なぜ?
リファレンスにちゃんと書いてあった...
オブジェクトが引数として渡されたり返り値となったり あるいは別の変数に代入されたりした場合、 それはエイリアスではありません。ID のコピーを保持し、 同じオブジェクトを指すようになるのです。
PHP: オブジェクトと参照 - Manual
というわけなので、代入せずにcloneしよう。
<?php $hoge = new stdClass(); $hoge->a = 1; $fuga = clone $hoge; $fuga->a = 2; print_r($hoge);
stdClass Object ( [a] => 1 )
知らなかった...