Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove apt_key and switch to deb822_respository #143

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 44 additions & 41 deletions library/visual_studio_code_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@


def is_extension_installed(module, executable, name):
rc, out, err = module.run_command([executable, '--list-extensions', name])
if rc != 0 or err:
rc, stdout, stderr = module.run_command([executable, '--list-extensions', name])
if rc != 0:
module.fail_json(
msg='Error querying installed extensions [%s]: %s' % (name,
out + err))
msg=f'Error querying installed extensions [{name}]',
rc=rc, stdout=stdout, stderr=stderr)
lowername = name.lower()
match = next((x for x in out.splitlines() if x.lower() == lowername), None)
return match is not None
match = next((x for x in stdout.splitlines() if x.lower() == lowername), None)
return match is not None, stdout, stderr


def list_extension_dirs(module, executable):
def list_extension_dirs(executable):
dirname = '.vscode'
if executable == 'code-insiders':
dirname += '-insiders'
Expand All @@ -37,47 +37,50 @@ def list_extension_dirs(module, executable):


def install_extension(module, executable, name):
if is_extension_installed(module, executable, name):
installed, installed_stdout, installed_stderr = is_extension_installed(module, executable, name)
if installed:
# Use the fact that extension directories names contain the version
# number
before_ext_dirs = list_extension_dirs(module, executable)
before_ext_dirs = list_extension_dirs(executable)
# Unfortunately `--force` suppresses errors (such as extension not
# found)
rc, out, err = module.run_command(
rc, stdout, stderr = module.run_command(
[executable, '--install-extension', name, '--force'])
# Whitelist: [DEP0005] DeprecationWarning: Buffer() is deprecated due
# to security and usability issues.
if rc != 0 or (err and '[DEP0005]' not in err):
if rc != 0:
module.fail_json(
msg='Error while upgrading extension [%s]: (%d) %s' %
(name, rc, out + err))
after_ext_dirs = list_extension_dirs(module, executable)
msg=f'Error while upgrading extension [{name}]',
rc=rc, stdout=installed_stdout+stdout, stderr=installed_stderr+stderr)
after_ext_dirs = list_extension_dirs(executable)
changed = before_ext_dirs != after_ext_dirs
return changed, 'upgrade'
if installed_stderr == stderr:
installed_stderr = ''
return changed, 'upgrade', installed_stdout+stdout, installed_stderr+stderr
else:
rc, out, err = module.run_command(
rc, stdout, stderr = module.run_command(
[executable, '--install-extension', name])
# Whitelist: [DEP0005] DeprecationWarning: Buffer() is deprecated due
# to security and usability issues.
if rc != 0 or (err and '[DEP0005]' not in err):
if rc != 0:
module.fail_json(
msg='Error while installing extension [%s]: (%d) %s' %
(name, rc, out + err))
changed = 'already installed' not in out
return changed, 'install'
msg=f'Error while installing extension [{name}]',
rc=rc, stdout=installed_stdout+stdout, stderr=installed_stderr+stderr)
changed = 'already installed' not in stdout
if installed_stderr == stderr:
installed_stderr = ''
return changed, 'install', installed_stderr+stderr


def uninstall_extension(module, executable, name):
if is_extension_installed(module, executable, name):
rc, out, err = module.run_command(
installed, installed_stdout, installed_stderr = is_extension_installed(module, executable, name)
if installed:
rc, stdout, stderr = module.run_command(
[executable, '--uninstall-extension', name])
if 'successfully uninstalled' not in (out + err):
if rc != 0:
module.fail_json(
msg=('Error while uninstalling extension [%s]:'
' unexpected response: %s') % (name, out + err))
return True
else:
return False
msg=(f'Error while uninstalling extension [{name}]'),
rc=rc, stdout=installed_stdout+stdout, stderr=installed_stderr+stderr)
if installed_stderr == stderr:
installed_stderr = ''
return True, installed_stdout+stdout, installed_stderr+stderr
return False, installed_stdout, installed_stderr


def run_module():
Expand Down Expand Up @@ -112,24 +115,24 @@ def run_module():
state = module.params['state']

if state == 'absent':
changed = uninstall_extension(module, executable, name)
changed, stdout, stderr = uninstall_extension(module, executable, name)

if changed:
msg = '%s is now uninstalled' % name
msg = f'{name} is now uninstalled'
else:
msg = '%s is not installed' % name
msg = f'{name} is not installed'
else:
changed, change = install_extension(module, executable, name)
changed, change, stdout, stderr = install_extension(module, executable, name)

if changed:
if change == 'upgrade':
msg = '%s was upgraded' % name
msg = f'{name} was upgraded'
else:
msg = '%s is now installed' % name
msg = f'{name} is now installed'
else:
msg = '%s is already installed' % name
msg = f'{name} is already installed'

module.exit_json(changed=changed, msg=msg)
module.exit_json(changed=changed, msg=msg, stdout=stdout, stderr=stderr)


def main():
Expand Down
17 changes: 8 additions & 9 deletions molecule/default/converge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,15 @@
name: '{{ visual_studio_code_extensions_dependencies }}'
state: present

- name: Install apt key (Debian)
ansible.builtin.apt_key:
url: 'https://packages.microsoft.com/keys/microsoft.asc'
state: present
when: ansible_pkg_mgr == 'apt'

- name: Install VS Code apt repo (Debian)
ansible.builtin.apt_repository:
repo: deb [arch=amd64] https://packages.microsoft.com/repos/vscode stable main
state: present
ansible.builtin.deb822_repository:
name: vscode
types: deb
architectures: amd64
uris: https://packages.microsoft.com/repos/vscode
suites: stable
components: main
signed_by: https://packages.microsoft.com/keys/microsoft.asc
when: ansible_pkg_mgr == 'apt'

- name: Install VS Code repo (yum, dnf)
Expand Down