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最後の夜)

2017年3月7日火曜日

インストール後に割り当てたディスクサイズを変更@centos7


■ 現状の確認
df -hT
/dev/mapper/centos-root xfs 50G /
/dev/mapper/centos-home xfs 200G /

今回は以下のように変更したい
/ 200G (150G増やす)
/home 削除
また、ファイルフォーマットを確認しておく。
(残りの50Gは余力として残しておく)

■ 今回やること
LVMのパーティション変更
ファイルシステムxfs にもサイズ変更を反映

■ home をumount
(必要に応じてhome以下をバックアップ)
umount /dev/mapper/centos-home
/etc/fstab を編集
# /dev/mapper/centos-home <- コメントアウトしておく

■ centos-home を削除
lvremove /dev/mapper/centos-home
lvdisplay でcentos-homeが消えていることを確認

■ centos-root のサイズ拡張
lvextend -L +150G /dev/centos/root
 ファイルシステムのサイズ拡張
xfs_growfs /dev/mapper/centos-root

■ 現状の確認
df -hT
/dev/mapper/centos-root xfs       200G  4.6G  196G   3% /
/home は消えている


参考サイト
http://iwamocchan11.hatenadiary.jp/entry/2016/02/08/001020#インストール後に変更したい場合
(CentOS6 について書かれている。CentOS7 と違う点、LVM関連の出力、ファイルシステム ext4, xfs)
https://access.redhat.com/documentation/ja-JP/Red_Hat_Enterprise_Linux/6/html/Storage_Administration_Guide/xfsgrow.html
XFS ファイルシステムのサイズの拡大 (xfs_growfsコマンド)

todo
 - lvdisplay, vgdisplay を調べる

2016年12月15日木曜日

systemd のservice に別名を付ける alias

サービス名にバージョン名が入ってほしくないので、
postgresql-9.6.service をpostgresql.service に変えられないかなと。。。

serviceに別名を付けるオプションがありました。
/usr/lib/systemd/system/postgresql-9.6.service の[Install]セクションに追加
[Install]
Alias=postgresql.service

service へのリンクを作り直します。
> systemctl daemon-reload
> systemctl disable postgresql.service
> systemctl enable postgresql.service
Created symlink from /etc/systemd/system/postgresql.service to /usr/lib/systemd/system/postgresql-9.6.service.
Created symlink from /etc/systemd/system/multi-user.target.wants/postgresql-9.6.service to /usr/lib/systemd/system/postgresql-9.6.service.

これで、postgresql.service という名前でsystemctl が利用できます。
> systemctl status postgresql.service
> systemctl status postgresql-9.6.service
どちらでも、内容は同じです。


2016年11月6日日曜日

pip v9.0.0 のエラー対処

pip をversion 9.0.0 にしたらエラーで困ってます

とりあえず、ダウングレードしておきます。

エラーメッセージ
>  File "/usr/local/lib/python3.4/site-packages/pip/_vendor/distro.py", line 922, in _lsb_release_info
>    stdout, stderr = stdout.decode('ascii'), stderr.decode('ascii')
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe3 in position 22: ordinal not in range(128)

まず、エラー個所を修正
/usr/local/lib/python3.4/site-packages/pip/_vendor/distro.py
の922行目を修正。
 stdout, stderr = stdout.decode('utf-8'), stderr.decode('utf-8')

次にダウングレード
pip install pip==8.1.1 --upgrade

しばらく、このバージョンで。

2016年10月7日金曜日

yum レポジトリ

超書きかけです。。。
https://ius.io/

todo
epel, postgres, nginx, mongo
(mariadbもいるかな?)