JaCoCo 实现原理 (JaCoCo Implementation Design)

想要对 Java 项目进行代码覆盖率的测试,很容易就找到 JaCoCo 这个开源代码覆盖率分析工具是众多工具中最后欢迎的哪一个。

本篇仅仅是在学习 JaCoCo 时对其实现设计文档 https://www.jacoco.org/jacoco/trunk/doc/implementation.html 的粗略翻译。

实现设计(Implementation Design)

这是实现设计决策的一个无序列表,每个主题都试图遵循这样的结构:

  • 问题陈述
  • 建议的解决方案
  • 选择和讨论

覆盖率分析机制(Coverage Analysis Mechanism)

覆盖率信息必须在运行时收集。为此,JaCoCo 创建原始类定义的插装版本,插装过程发生在加载类期间使用一个叫做 Java agents 动态地完成。

有几种收集覆盖率信息的不同方法。每种方法都有不同的实现技术。下面的图表给出了 JaCoCo 使用的技术的概述:

实现

字节码插装非常快,可以用纯 Java 实现,并且可以与每个 Java VM 一起工作。可以将带有 Java 代理钩子的动态插装添加到 JVM 中,而无需对目标应用程序进行任何修改。

Java 代理钩子至少需要 1.5 个 JVMs。用调试信息(行号)编译的类文件允许突出显示源代码。不幸的是,一些 Java 语言结构被编译成字节代码,从而产生意外的突出显示结果,特别是在使用隐式生成的代码时(如缺省构造函数或 finally 语句的控制结构)。

Read More

Ansible 实践

最近在思考如何将团队里的所有的虚拟机都很好的管理并监控起来,但是由于我们的虚拟机的操作系统繁多,包括 Windows, Linux, AIX, HP-UX, Solaris SPARC 和 Solaris x86. 到底选择哪种方式来管理比较好呢?这需要结合具体场景来考虑。

Ansible 和其他工具的对比

这里有一个关于 Chef,Puppet,Ansible 和 Saltstack 的对比文章

https://www.edureka.co/blog/chef-vs-puppet-vs-ansible-vs-saltstack/

选择合适的工具

仅管理 Windows 和 Linux

如果你的虚拟机没有这么多平台,只是 Windows 和 Linux,假如你已经有了 VMware vSphere 来管理了,那么可以通过 VMware vSphere API 来查看这些机器的状态。

这里是 VMware 官方的 API Library 供使用:

管理多个操作系统

如果你和我的情况一下,想监控很多个操作操作系统,那么就只能通过 ssh 来登录到每一台机器上去查看,比如执行 uptime 等命令。可以写 shell 脚本来完成这些登录、检测等操作。

另外就是使用 Ansible 的 Playbook。Playbook 里描述了你要做的操作,这是一个权衡,学习 Ansible 的 Playbook 需要花些时间的。

如果想了解下 Ansible 那么可以试试 Ansible Playbook。以下是我使用 Ansible 做了一些练习。

Playbook结构

+- vars
| +- vars.yml
| +- ...
+- hosts # save all hosts you want to monitor
+- run.yml # ansible executable file

Playbook具体代码

Read More

写给那些想使用 JFrog Artifactory 管理制品的人

我在使用 Artifactory 做持续集成已经有一段时间了,对企业级 Artifactory 也有了一些经验和总结,希望能通过本篇的分享帮助刚接触这个工具的人了解什么是Artifactory,它能做什么,为什么要选择它,以及在使用过程中应该注意什么。

什么是Artifactory

一句话概括:Artifactory 是一个存放制品(Artifacts)的工具。当前,Artifactory 是一个非常有影响力,功能非常强大的工具。

Artifactory有哪些优势

可能你的团队已经有了自己的管理制品的方式,比如 FTP 等。Artifactory 能带来什么呢?让我先来看看它有哪些优势。

注:以下优势都是针对 JFrog Aritifacvtory 企业版来介绍的。开源版,即 OSS 版本不具备以下丰富的功能。

优势1:它是一个通用管理仓库

JFrog Artifactory 企业版完全支持所有主要包格式的存储库管理器。它不但可以管理二进制文件,也可以对市面上几乎所有语言的包的依赖进行管理,如下图所示

主要的包格式

因此,使用 Artifactory 能够将所有的二进制文件和包存储在一个地方

Read More

程序员自我修养之Git提交信息和分支创建规范

为什么要制定规范

古话说,没有规矩不成方圆。在团队协作开发时,每个人提交代码时都会写 commit message,但如果没有规范,每个人都会有自己的书写风格,因此在翻看 git log 时经常看到的是五花八门,十分不利于阅读和维护。

通过下面两个例子来看看没规范和有规范的对比,以及有规范能带来哪些好处。

提交信息 没规范 vs 有规范

没有规范的 Git 提交信息

从这个提交信息里你不知道他修改了什么,修改意图是什么。

Read More

在 GitHub 上发布一个 Python 项目需要注意哪些

本篇介绍个人或企业在 GitHub 上发布一个 Python 项目需要了解和注意哪些内容

  1. 如何配置setup.py
  2. 如何发布到PyPI
  3. 生成pydoc
  4. 版本号的选择
  5. License的选择

配置setup.py

打包和发布一项都是通过准备一个 setup.py 文件来完成的。假设你的项目目录结构如下:

demo
├── LICENSE
├── README.md
├── MANIFEST.in # 打包时,用来定制化生成 `dist/*.tar.gz` 里的内容
├── demo
│ └── __init__.py
├── setup.py
├── tests
│ └── __init__.py
│ └── __pycache__/
└── docs

在使用打包命令 python setup.py sdist bdist_wheel,将会生成在 dist 目录下生成两个文件 demo-1.0.0-py3-none-any.whldemo-1.0.0.tar.gz

  • .whl 文件是用于执行 pip install dist/demo-1.0.0-py3-none-any.whl 将其安装到 ...\Python38\Lib\site-packages\demo 目录时使用的文件。

  • .tar.gz 是打包后的源代码的存档文件。而 MANIFEST.in 则是用来控制这个文件里到底要有哪些内容。

Read More

About Python pip install and versioning

Backgroup

If you want to release python project on PyPI, you must need to know about PyPI usage characteristics, then I did some test about pip install command.

For example: I have a Python project called demo-pip. and beta release would like 1.1.0.xxxx, offical release version is 1.1.0 to see if could success upgrade when using pip command.

Base on the below test results, I summarized as follows:

  1. Install a specific version of demo-pip from PyPI, with --upgrade option or not, they’ll all both success.
  2. Install the latest package version of demo-pip from PyPI that version is large than the locally installed package version, with --upgrade option installs successfully. without --upgrade option install failed.
  3. Install the latest package version of demo-pip from PyPI that version is less than the locally installed package version, with --upgrade option or not, install failed.
  4. 1.1.0.xxxx version naming is OK, but when the beta version is larger than 1.1.0, for example, the beta version is 1.1.0.1000, pip install with --upgrade not work when our official release version is 1.1.0.
    a. One option is the official release version start from 1.1.0.1000, beta version starts from 1.1.0.0001, 1.1.0.0002… Or the beta version should be less than 1.1.0, maybe 1.0.0.xxxx
    b. Another option is follow up python official versioning that is the best practice, then the beta release version will be 1.1.b1, 1.1.b2, 1.1.bN… (it passed No.5 test below)

My Test Case

Read More

Update Jira server account avatar with rest API

Backgroud

When you are using a server account for CI/CD, if you want to make the server account avatar to looks professional on Jira update but the server account may not allowed to log to Jira, so you can not update the avatar though GUI, you could use Jira REST API to do this.

I assume you have an account called robot, here are the examples of how to update though REST API.

Example in Python

import http.client

conn = http.client.HTTPSConnection("jira.your-company.com")

payload = "{\r\n\t\"id\": \"24880\",\r\n\t\"isSelected\": false,\r\n\t\"isSystemAvatar\": true,\r\n\t\"urls\": {\r\n\t\t\"16x16\": \"https://jira.your-company.com/secure/useravatar?size=xsmall&avatarId=24880\",\r\n\t\t\"24x24\": \"https://jira.your-company.com/secure/useravatar?size=small&avatarId=24880\",\r\n\t\t\"32x32\": \"https://jira.your-company.com/secure/useravatar?size=medium&avatarId=24880\",\r\n\t\t\"48x48\": \"https://jira.your-company.com/secure/useravatar?avatarId=24880\"}\r\n}"

headers = {
'content-type': "application/json",
'authorization': "Basic Ymx3bXY6SzhNcnk5ZGI=",
'cache-control': "no-cache",
'postman-token': "ecfc3260-9c9f-e80c-e3e8-d413f48dfbf4"
}

conn.request("PUT", "/rest/api/latest/user/avatar?username=robot", payload, headers)

res = conn.getresponse()
data = res.read()

print(data.decode("utf-8"))

Example in Postman

Read More

Fixed "Remote session was disconnected because there are no Remote Desktop client access licenses available"

Problem

Sometimes my Windows server 2012 R2 has RDP connect problem below:

The remote session was disconnected because there are no Remote Desktop client access licenses available for this computer.
Please contact the server administrator.

RDP connect problem

Read More

通过 generic-webhook-trigger 插件实时获取 Bitbucket Repository Events

背景

本篇讨论如何通过 Jenkins generic webhook trigger 插件来获取 Git 仓库事件(Events)。比如获取仓库的 Pull Request ID 等。

使用过 Jenkins Multi-branch pipeline Job 的用户知道,这个 Job 类型的环境变量中可以得到 Pull Request 的相关信息如下

Multi-branch pipeline Job 环境变量

为了获取这个变量需要创建这种类型的 Job,并且可能需要 clone 该仓库的代码,有点杀鸡宰牛的意思,看起来并不是一个特别恰当的办法。

如何通过创建一个普通的 Jenkins Job 就能实时获取 Bitbucket 仓库以及 Pull Request 事件呢?通过以下功能和插件可以实现。

Read More

Annual work summary from 2019.03 - 2020.07

Summarize what did I do from 2019.03 to 2020.07 when I became a Build Release/DevOps engineer.

Build automation

  • Support all server windows platform build manual to auto.
  • Support clients build from manual to auto.
  • Switch Linux/Unix build from Bamboo to Jenkins.
  • Support all platforms branches/Pull Request build.
  • Provide auto-build as self-service for a developer, no need to involve build engineer, they could build themselves.

Integration

  • Integration with Jenkins

    • Self-service installation.
    • Blackduck, Polaris integration.
    • Git stats, analyze Bitbucket data with Elastic stack.
    • Monitor legacy build machines status.
    • Product escrow, sync xdemo, provide NFS and SYNC mvopensrc, update Bitbucket Jenkins build status, etc.
  • Integration with JFrog Ariifactory

    • Establish deploy strategy and directory structure organization.
    • Handle artifacts(build, etc) with different maturity.

Infrastructure management

  • Manage Jenkins for setting, update and backup.
  • Ariifactory artifacts cleanup, retention, backup.
  • Git branches/hooks management.
  • VMs tracking, management build machines.