2019年4月23日火曜日

lvm でswap サイズを変更

メモリを追加したのでスワップも拡張
4GB -> 16GB CentOS7
スワップがどのくらい必要かわからないので、とりあえず10GBほどにしておく。


# ボリュームの確認
$ lvscan 
  ACTIVE            '/dev/centos/swap' [<3.88 GiB] inherit
  ACTIVE            '/dev/centos/root' [200.00 GiB] inherit

$ swapoff -v /dev/centos/swap
swapoff /dev/centos/swap

# スワップ用ボリュームのリサイズ。6GB 追加
$ lvresize -L +6G /dev/centos/swap 
  Size of logical volume centos/swap changed from <3.88 GiB (992 extents) to <9.88 GiB (2528 extents).
  Logical volume centos/swap successfully resized.

# リサイズ後の確認
$ lvscan 
  ACTIVE            '/dev/centos/swap' [<9.88 GiB] inherit
  ACTIVE            '/dev/centos/root' [200.00 GiB] inherit

# スワップ作成
$ mkswap /dev/centos/swap
mkswap: /dev/centos/swap: warning: wiping old swap signature.
Setting up swapspace version 1, size = 10354684 KiB
no label, UUID=172f0357-4a83-477e-a364-9023e6e28e2e

# 
$ swapon -v /dev/centos/swap
swapon /dev/centos/swap
swapon: /dev/mapper/centos-swap: found swap signature: version 1, page-size 4, same byte order
swapon: /dev/mapper/centos-swap: pagesize=4096, swapsize=10603200512, devsize=10603200512

# 確認
$ cat /proc/swaps
$ free -h


■ redhat7 のドキュメント
- 論理ボリュームマネージャーの管理
- CHAPTER 15. SWAP SPACE

2019年4月14日日曜日

コード表示を改善

このブログ、プログラム中心のブログなのに、何の色付けもされていないので見づらい。
今更だけど、ほんのりと色指定をしてみました。

スタイルを追加
<style type='text/css'>
  pre.code{color:#ccc; background-color:#2d2d2d; padding:0.5em 1em; white-space: pre-wrap; word-wrap: break-word;}
</style>
背景色を黒にしただけです。 (かなりの手抜きだぁ)
highlight系のjs,css は見送りました。大変そうなので...

いままでは、こんな感じでした。
#!/usr/bin/env python

print('hi python')

変更後
#!/usr/bin/env python

print('hi python')
すこーしだけいい感じになったかな (;'∀')

今回は、preタグを直接書き込みましたが、Bloggerの編集モードと相性悪いですね。
改行<br />, スペース &nb; が自動挿入されるので、pre内は自分で消すしかないのかな?

2019年4月7日日曜日

splash でuser-agent を設定

splash のリクエストでuser-agent を設定する。
目的のサイト <-uaを指定- splash <-- python(requests)

# ここを参考
https://splash.readthedocs.io/en/stable/faq.html

ローカルのsplashサーバーへはrun、メソッドはPOST を指定。
(もっとドキュメントを読む必要あり.....)

以下の2点は確認済み
 - リスエストのヘッダーを見ると、期待したuser-agentが設定されている。
 - メソッドはGETになっている。(たぶんデフォがGET)

import requests

def splash_scripts():
  return """
splash:set_user_agent(args.ua)
splash:go(args.url)
return splash:html()
"""

def req_page(url):
  ua = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36'

  url_local = 'http://localhost:8050/run'
  prms = {
    'lua_source': splash_scripts(),
    'url': url,
    'ua': ua,
    # 'wait': 2
  }
  res = requests.post(url_local, json=prms)
  return res

if __name__ == '__main__':
  # リスエストのヘッダーを確認
  url = 'http://tools.up2a.info/ja/requestheaders'
  # url = 'http://192.168.1.3/'
  res = req_page(url)
  print(res.text)

2019年1月21日月曜日

Xvfb

# yum install xorg-x11-server-Xvfb

# export DISPLAY=:1
# Xvfb :1 -screen 0 1280x1024x24 &

# killall Xvfb

参考
https://qiita.com/kt3k/items/cea3c6de3c2337004a84
https://en.wikipedia.org/wiki/Xvfb

必要ならsystemd で自動起動
https://qiita.com/ine1127/items/363b597f895c6663ddb7

2018年8月10日金曜日

dataclassデコレータ

Python3.7 で追加されたdataclassデコレータについて。

__init__ を書かなくても済むのはいいけど、型情報を書く必要ある。
これは慣れていくしかないか...
というか、class の書き方もこのスタイルに変わってくるかも。

from dataclasses import dataclass

@dataclass
class Point:
    x: float
    y: float
    z: float = 0.0

p = Point(1.5, 2.5)
print(p)   # Point(x=1.5, y=2.5, z=0.0)
@dataclass(frozen=True) でイミュータブルになり、少しだけパフォーマンスが良くなると。


What’s New In Python 3.7
PEP 557 – Data Classes

2018年5月11日金曜日

コアサーバー借りました

バリュードメインのコアサーバー借りました。
(30日のお試し期間中ですけど)

時代は dockerで、コンテナだとかOpenShift だとか言ってるけど、こんな時代だから共用サーバーを使ってみる!
さくらのArukas なんかと比べると3周くらい遅れてるゾ。(;'∀')

サーバ側の実行環境はphp, fastcgi, apache が使えます。
必要があれば、PHPで頑張るつもりで契約しました。
(もちろん、CGI も使えますよ)

■ python
php (WordPress, Laravelなど)の利用を想定していますが、何故かpyenv を入れてみました。
最新のver3.6はpip のエラーでインストールできませんでした。
3.3, 3.5, 3.7beta はすんなりとインストールできます。
python3.6 だけ、pip/_vendor/distro.py が必要で、その中から/etc を参照していますが、コアサーバーでは
PermissionError: [Errno 13] Permission denied: '/etc'
とエラーで終わります。
https://github.com/pypa/pip/blob/10.0.1/src/pip/_vendor/distro.py

あと2カ月ほどでver3.7 もリリースされるので個人的には問題なしです。

■ CGI の確認
wsgiref.handlers.CGIHandler() を利用。
py.cgi として以下を作成。
#!/[home-path]/.pyenv/shims/python3.7
import wsgiref.handlers
import sys

def application(environ, start_response):
    start_response('200 OK', [('Content-Type', 'text/plain')])
    res = ('Hello World!\n' + sys.version).encode("utf-8")
    return [res]

if __name__ == '__main__':
    wsgiref.handlers.CGIHandler().run(application)

事前に作られたディレクトリ、~/public_html/exsample.com/に置くだけでOKでした。
(あとCGIなので、chmod 711 も忘れずに)
ブラウザでアクセスするとちゃんと表示されました。
(拡張子をpy にしたら AddHandler cgi-script .py が必要かもしれません)

Hello World!
3.7.0b4 (default, May 11 2018, 22:03:04)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)]

このメモを書いてて思ったんですが .htaccess の設定すれば、おそらくpython(flup), ruby(rack) でもfastcgi 利用できますね。(たぶんですよ)

2018年5月7日月曜日

lxc のインストール (Centos7)


何となくなんですけど、LXCを使ってみることにしました。
稼働しているLinux がCentOS7だけなので、CentOSに。 
rpm パッケージは無いようなので、ソースからインストールしてみした。

■準備
以下をインストールしておかないと、怖いよ~
yum install  libacl-devel libcap-devel dnsmasq squashfs-tools libtool

■ LXC のインストール
バージョン3.0.0は1ヶ月前にリリースされたばかり。とりあえず、最新に乗ってみる。
https://github.com/lxc/lxc のREADME に書かれている通りにやってみる。
(っていうか、普通にconfigure, makeです)
autogen.sh の実行で libtoolize がないとエラーが出たのでインストール
(yum install libtool)
インストール先は/opt

# ./autogen.sh
# ./configure --prefix=/opt/lxc-3.0.0
# make && sudo make install

# cd /opt
# ln -s lxc-3.0.0 lxc

/opt/lxc/bin をパスに追加しておく

# systemctl start lxc-net
# systemctl start lxc

■ コンテナ作成
fedora28 が使いたかったけどまだないようなのでfedora27にしました。
# lxc-create -t lxc-download fed27
(fed27 はコンテナ名。なんでもいい)

# lxc-start fed27
lxc-start fed27 20180506133710.205 ERROR    lxc_utils - utils.c:run_command:2250 - failed to exec command
lxc-start fed27 20180506133710.206 ERROR    lxc_network - network.c:lxc_ovs_attach_bridge:1890 - Failed to attach "lxcbr0" to openvswitch bridge "veth7JIGEE": lxc-start: fed27: utils.c: run_command: 2250
lxc-start fed27 20180506133710.206 ERROR    lxc_network - network.c:instantiate_veth:194 - Failed to attach "veth7JIGEE" to bridge "lxcbr0": Operation not permitted
lxc-start fed27 20180506133710.217 ERROR    lxc_network - network.c:lxc_create_network_priv:2436 - Failed to create network device
lxc-start fed27 20180506133710.217 ERROR    lxc_start - start.c:lxc_spawn:1545 - Failed to create the network
lxc-start fed27 20180506133710.217 ERROR    lxc_start - start.c:__lxc_start:1866 - Failed to spawn container "fed27"

エラーが出て失敗。
ここをみて、ifcfg-lxcbr0 を追加、ifup lxcbr0 で上のエラーはクリアー。
https://unix.stackexchange.com/questions/179353/failed-to-attach-veth87vsij-to-the-bridge-virbr0-no-such-device
(実際は、systemctl start lxc-net を実行する前に lxc-start を実行したからいけなかったのかもしれない)

あらためて
# lxc-start fed27
別のエラー発生!
lxc-start fed27 20180506135716.874 ERROR    lxc_conf - conf.c:setup_caps:2404 - unknown capability mac_admin
lxc-start fed27 20180506135716.874 ERROR    lxc_conf - conf.c:lxc_setup:3475 - Failed to drop capabilities
lxc-start fed27 20180506135716.874 ERROR    lxc_start - start.c:do_start:1198 - Failed to setup container "fed27"
lxc-start fed27 20180506135716.874 ERROR    lxc_sync - sync.c:__sync_wait:57 - An error occurred in another process (expected sequence number 5)
lxc-start fed27 20180506135716.887 ERROR    lxc_start - start.c:__lxc_start:1866 - Failed to spawn container "fed27"

# yum install libcap-devel
もう一度 make, make install をやり直す。

あらためて
# lxc-start fed27
エラー? フリーズ?
systemd 234 running in system mode. (+PAM +AUDIT +SELINUX +IMA -APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ +LZ4 +SECCOMP +BLKID +ELFUTILS +KMOD -IDN2 +IDN default-hierarchy=hybrid)
Detected virtualization lxc.
Detected architecture x86-64.

Welcome to Fedora 27 (Twenty Seven)!

Set hostname to <fed27>.
Cannot determine cgroup we are running in: No medium found
Failed to allocate manager object: No medium found
[!!!!!!] Failed to allocate manager object, freezing.
Freezing execution.

色々調べてみたけれど、どうにもならない。。。
最後の苦しまぎれ.....  lxc を止めて、コンテナ作り直してみる。
# systemctl stop lxc
# systemctl start lxc
# lxc-destroy fed27
もう一度、コンテナ作って lxc-start したらエラーが消えましたwww
もうあきらめようと思っていましたよぉ ほんとに(涙

起動が早すぎて喜ぶひまもなかった。さすがコンテナ!
lxc-attach でコンテナfed27のターミナルに移れました。
(ssh でも入れたのかな?)
# cat /etc/redhat-release
Fedora release 27 (Twenty Seven)

力尽きたので、今日はこれでおしまい。
(GW最後の夜)