무척 간결한 앤서블의 jenkins_script  모듈이다.

혹시 젠킨스에서 암호 없이 접근한데 4xx 에러가 난다면. 이는 분명 proxy 이슈이다.



https://github.com/ansible/ansible/pull/19640/files


from ansible.module_utils.basic import AnsibleModule

from ansible.module_utils.urls import fetch_url

try:

    # python2

    from urllib import urlencode

except ImportError:

    # python3

    from urllib.parse import urlencode


def main():


    module = AnsibleModule(

        argument_spec = dict(

            script = dict(required=True, type="str"),

            url   = dict(required=False, type="str", default="http://localhost:8080"),

            validate_certs = dict(required=False, type="bool", default=True),

            user   = dict(required=False, no_log=True, type="str",default=None),

            password   = dict(required=False, no_log=True, type="str",default=None),

            args   = dict(required=False, type="dict", default=None)

        )

    )



    if module.params['user'] is not None:

        if module.params['password'] is None:

            module.fail_json(msg="password required when user provided")

        module.params['url_username'] = module.params['user']

        module.params['url_password'] = module.params['password']

        module.params['force_basic_auth'] = True



    if module.params['args'] is not None:

        from string import Template

        script_contents = Template(module.params['script']).substitute(module.params['args'])

    else:

        script_contents = module.params['script']



    resp, info = fetch_url(module,

                           module.params['url'] + "/scriptText",

                           data=urlencode({'script': script_contents}),

                           method="POST")


    if info["status"] != 200:

        module.fail_json(msg="HTTP error " + str(info["status"]) + " " + info["msg"])


    result = resp.read()


    if 'Exception:' in result and 'at java.lang.Thread' in result:

        module.fail_json(msg="script failed with stacktrace:\n " + result)


    module.exit_json(

        output = result,

    )




if __name__ == '__main__':

    main()


Posted by '김용환'
,

앤서블로 jenkins 플러그인 목록을 웹이 아닌 API 형태로 출력하려면 다음과 같이 호출할 수 있다.


- name : get plugins
environment:
no_proxy: "localhost"
uri:
url: "http://localhost:8080/scriptText"
method: POST
return_content: yes
HEADER_Content-Type: "application/json"
body: "println(Jenkins.instance.pluginManager.plugins)"


Posted by '김용환'
,


DNS는 하나 이상의 레코드를 호스트 이름과 연결해 동작한다. 가장 일반적으로 사용되는 DNS 레코드 타입은 A 레코드와 CNAME 레코드다. A 레코드는 호스트 이름을 IP 주소에 연결하는 것이고, CNAME 레코드는 호스트 이름을 다른 호스트 이름으로 앨리어스로 지정한 것을 말한다.

DNS 프로토콜은 TXT 레코드라는 호스트 이름과 연결할 수 있는 다른 타입의 레코드를 지원한다. TXT 레코드는 호스트 이름에 연결할 수 있는 임의의 문자열이다. TXT 레코드를 호스트 이름과 연결하면 누구나 DNS 클라이언트를 사용해 텍스트를 검색할 수 있다.


예를 들어 github.com 도메인을 소유하고 있기에 해당 도메인의 모든 호스트 이름과 관련된 TXT 레코드를 생성할 수 있다. TXT 레코드를 이 책의 ISBN 번호가 포함된 github.com 호스트 이름과 연결했다. 

dig 커맨드라인 툴을 사용해 TXT 레코드를 조회할 수 있다.

$ dig +short github.com TXT

"MS=ms44452932"

"docusign=087098e3-3d46-47b7-9b4e-8a23028154cd"

"v=spf1 ip4:192.30.252.0/22 ip4:208.74.204.0/22 ip4:46.19.168.0/23 include:_spf.google.com include:esp.github.com include:_spf.createsend.com include:servers.mcsv.net ~all"

"MS=6BF03E6AF5CB689E315FB6199603BABF2C88D805"

"MS=ms58704441"



dnstxt 조회는 DNS 서버에 호스트와 관련된 TXT 레코드를 쿼리한다. 플레이북에서 이와 같은 태스크를 생성한다.


- name: look up TXT record

debug: msg="{{ lookup('dnstxt', 'github.com') }}"






Posted by '김용환'
,


젠킨스를 설치한 후 바로 plugin을 다운받을 수 없다. connection timeout이 발생할 것이다.


젠킨스(jenkins) 플러그인을 다운받으려면 수동으로 다음을 진행한다. 


1. 먼저

Manage Jenkins -> Configure Global Security -> Plugin Manager and check the box for Use browser for metadata download.


2. plugin 관리에서 Proxy 서버를 추가해야 한다.

https://wiki.jenkins.io/display/JENKINS/JenkinsBehindProxy



Posted by '김용환'
,


war 파일의 jenkins를 처음 실행하면 




https://wiki.jenkins.io/display/JENKINS/Features+controlled+by+system+properties


-Djenkins.install.runSetupWizard=false



Set to false to skip install wizard. Note that this leaves Jenkins unsecured by default. 

Development-mode only: Set to true to not skip showing the setup wizard during Jenkins development.


NOTE: This property is only effective the first time you run Jenkins in given JENKINS_HOME.


war 파일을 사용하는 관점에서 JENKINS_HOME 디렉토리가 이미 존재한 상태에서 -Djenkins.install.runSetupWizard=false에 jenkins jvm 옵션에 적용 -Djenkins.install.runSetupWizard=false에 jenkins jvm 옵션에 적용하면 계속 setup wizard 화면이하면 계속 setup wizard 화면이 나온다.

즉 JENKINS_HOME이 이미 존재한 상태라면 해당 디렉토리를 지우고(잠깐 비우거나 함) 


Posted by '김용환'
,



앤서블을 사용해 압축 파일을 복사하려고 하는데 에러가 발생했다.

copy: src={{downlod_result_jenkins.dest}}
dest={{service_path}}/jenkins
remote_src=no
group={{default_user}}
owner={{default_group}}

FAILED! => {"changed": false, "msg": "Could not find or access '/file.war' on the Ansible Controller.\nIf you are using a module and expect the file to exist on the remote, see the remote_src option"}




https://github.com/ansible/ansible/issues/31926을 보니 일부러 앤서블에서 막은 듯 해서.

아래와 같이 압축 파일 복사는 다음과 같이 command 모듈을 사용해 진행했다.


- name : jenkins 복사 - {{service_path}}/jenkins
command: cp -r {{downlod_result_jenkins.dest}}\ {{service_path}}/jenkins


Posted by '김용환'
,



변수 출력하기

- command: "ls {{downlod_result_jenkins.dest}}"
register: dir_out

- debug: var={{item}}
with_items: dir_out.stdout_lines


Posted by '김용환'
,



특정 파일을 제외하고 하위 디렉토리의 모든 디렉토리와 파일을 지울 때..

물어보길 원할 때 다음과 같은 커맨드를 사용하면 유용하다.


$ find . -maxdepth 1 -type d -not -name "google" -not -name "gift-wine*" -exec rm -ri {} \;


결과는 다음 화면이 나온다.

rm: "." and ".." may not be removed

하나씩 물어봄


Posted by '김용환'
,


Waiting for table metadata lock 재현하기 


이 때 의도적으로 지연해야 하기 sleep 함수를 사용한다.

https://dev.mysql.com/doc/refman/8.0/en/miscellaneous-functions.html#function_sleep


* A session - 일반 계정


start transaction;

select * from target_ids_job_execution where sleep(1000)=0;


* B session - root


MariaDB [test]> ALTER TABLE target_ids_job_execution  add column xxx char(32) default 'xyz';

행(hang)


* C Session - root

show processlist; 



sleep을 포함한 sql 쿼리가 끝나도 여전히 alter table은 hang 걸려 있다. lock에 잡혀 있다. 



따라서 KILL 6을 실행해 metadata lock에 걸려 있는 쿼리를 종료시킨다.


show processlist를 실행하면 다음과 같은 결과가 나타난다.






Posted by '김용환'
,

jenkins 장비의 플러그인 목록을 형상관리 안하는 경우가 많다.. 플러그인 설정이 있어도 플러그인이 없으면 복구 할 수 없다. 


jenkins 장비의 플러그인 목록을 보려면 http://장비명:8080/script에 다음 커맨드를 실행한다.


Jenkins.instance.pluginManager.plugins.each{

  plugin -> 

    println ("${plugin.getShortName()}: ${plugin.getVersion()}")

}


결과는 다음과 같다.


Git plugin (git): 3.9.1
JavaScript GUI Lib: ACE Editor bundle plugin (ace-editor): 1.1
SCM Sync Configuration Plugin (scm-sync-configuration): 0.0.10
Pipeline: Job (workflow-job): 2.24

..


jenkins master를 제대로 복구할 때, 

플러그인 이름과 버전, jenkins 버전.. 이 매우 중요하다.

Posted by '김용환'
,