LoginSignup
2
1

More than 3 years have passed since last update.

Ubuntu18.04LTSでpython3でawslogsを動かす

Posted at

みなさん、こんにちは。
306です。
awslogs使ってますか?
awslogsはEC2のログ(syslog)などを、AWSのCloudWatchlogsに流す便利なソフトです。

導入方法は公式ドキュメントに書いてあるんですが、

もともと、AmazonLinux向けに作られているみたいで
Ubuntuに入れるには、怪しいPythonスクリプトを実行して入れます。
しかし、Ubuntu18だとデフォルトでPython3.6が入っており、
このスクリプトは、3.5までしか対応しておらずエラーが出て導入できません。
これは軽く修正すればよかったんですが、そのまま動かすとpython2が入ってしまいます。
(pipコマンドでpython入ってるかみているのと、Ubuntu18はpipではなくpip3で動くので、pythonが入っていないと判断されてapt install python-pip(python2とpip)がインスコされます)
awslogsもpython3で動かそうと思ったところ結構躓いたのでメモします。
一応、Python2入ってもいいよ人向けと
Python3で頑張るよ人向けに書きます。

python2でもいいよ

ソースのPythonのバージョン判定の部分をいじります。


@@ -1293,7 +1293,7 @@ def main():
         fail("This script doesn't support Microsoft Windows", PLATFORM_NOT_SUPPORTED)

     python_version = sys.version_info
-    if python_version < (2,6) or python_version >= (3,6):
+    if python_version < (2,6) or python_version >= (3,7):
         fail("This script only supports python version 2.6 - 3.5", PLATFORM_NOT_SUPPORTED)

     if options.only_generate_config and options.non_interactive:

これだけです。はい。
あとは、

sudo python3 ./awslogs-agent-setup.py -r ap-northeast-1

として、やってやればおkです。

Python3で頑張るよ人向け

バージョン判定とpip呼んでいるところを、pip3に置き換えます。
awslogs-agent-setup.py.meが私の書いたやつです。

diff --git a/awslogs-agent-setup.py b/awslogs-agent-setup.py.me
index 3a60ba7..c561bb5 100644
--- a/awslogs-agent-setup.py
+++ b/awslogs-agent-setup.py.me
@@ -260,10 +260,10 @@ for file in `cat ${FILES}`; do
 done

 echo "CloudWatch Logs Plugin Version:"
-/var/awslogs/bin/pip show awscli-cwlogs
+/var/awslogs/bin/pip3 show awscli-cwlogs

 echo "AWS CLI Version:"
-/var/awslogs/bin/pip show awscli
+/var/awslogs/bin/pip3 show awscli
 """


@@ -664,9 +664,9 @@ class CloudWatchLogsAgentSetup:
         defaults to the path of the dependency folder unless an explicit
         folder_path is passed in.
         """
-        pip_path = AWSLOGS_BIN + '/pip'
+        pip_path = AWSLOGS_BIN + '/pip3'
         if not virtual_env_pip:
-            pip_path = 'pip'
+            pip_path = 'pip3'

         install_cmd = [pip_path, 'install']

@@ -728,25 +728,25 @@ class CloudWatchLogsAgentSetup:
     def _install_ubuntu_dependencies(self):
         if self.os_flavor == self.Ubuntu or self.os_flavor == self.Debian:
             libyaml_dev_installed = self._package_installed("libyaml-dev")
-            python_dev_installed = self._package_installed("python-dev")
+            python_dev_installed = self._package_installed("python3-dev")
             if libyaml_dev_installed and (not python_dev_installed):
-                self.install("python-dev")
+                self.install("python3-dev")

     def install_pip(self):

         # For standalone installation, no need to check for the existence
         # of pip, we will simply be using the virtualenv packages in the
         # dependency folder to activate AWSLOGS_HOME as the virtualenv.
-        if not self.is_standalone and not executable_exists("pip"):
+        if not self.is_standalone and not executable_exists("pip3"):
             if self.os_flavor == self.Rhel or self.os_flavor == self.CentOS:
                 self.install("python-setuptools")
                 subprocess.call(['easy_install', 'pip'], stdout=self.log_file, stderr=self.log_file)
             else:
-                self.install("python-pip")
+                self.install("python3-pip")

         # If this is not standalone installation and pip executable still
         # doesn't exist, we have a problem
-        if not self.is_standalone and not executable_exists("pip"):
+        if not self.is_standalone and not executable_exists("pip3"):
             fail("Could not install pip. Please try again or see " + AGENT_SETUP_LOG_FILE + " for more details")

     def _package_installed(self, package_name):
@@ -1293,7 +1293,7 @@ def main():
         fail("This script doesn't support Microsoft Windows", PLATFORM_NOT_SUPPORTED)

     python_version = sys.version_info
-    if python_version < (2,6) or python_version >= (3,6):
+    if python_version < (2,6) or python_version >= (3,7):
         fail("This script only supports python version 2.6 - 3.5", PLATFORM_NOT_SUPPORTED)

     if options.only_generate_config and options.non_interactive:

そして失敗しますが以下のコマンドを叩きます

sudo python3 ./awslogs-agent-setup.py -r ap-northeast-1

途中までいって失敗します。
そうすると/var/awslogs/binに移動してみると、不穏なファイルがたくさんあります。
どうも、元々入っているpython3のバイナリ周りをコピーしてきているようです。
pipが壊れているので入れ直します。

sudo wget https://bootstrap.pypa.io/get-pip.py
sudo ./python3 get-pip.py

これでpipが入りました。
あとは、

sudo ./pip install awscli awscli-cwlogs

としてawslogsに必要なライブラリを導入して

sudo python3 ./awslogs-agent-setup.py -r ap-northeast-1

として再インストールすればPython3で動くようになりました!
お疲れ様でした!

一言

こんな怪しげで動作が不安ななスクリプトを公式が提供しないでほしいです。

2
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1