Skip to content

Commit a9df4f8

Browse files
Install default azure extensions (#1566)
Fixes OPS-2673 --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 910106c commit a9df4f8

5 files changed

Lines changed: 88 additions & 7 deletions

File tree

.devcontainer/Dockerfile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ RUN <<-```
1818
ENV LANG en_US.UTF-8
1919
ENV LANGUAGE en_US:en
2020
ENV LC_ALL en_US.UTF-8
21+
ENV AZURE_CONFIG_DIR="/tmp/azure"
22+
ENV AZURE_EXTENSION_DIR="/opt/azure/config/cliextensions"
2123

2224
RUN <<-```
2325
# Install yq
@@ -38,6 +40,17 @@ RUN <<-```
3840

3941
# Install Azure CLI
4042
curl -sL https://aka.ms/InstallAzureCLIDeb | bash
43+
mkdir -p /tmp/azure
44+
mkdir -p /opt/azure/config/cliextensions
45+
46+
# Configure Azure CLI extensions
47+
az config set extension.use_dynamic_install=yes_without_prompt
48+
az extension add --name reservation --only-show-errors || true
49+
az extension add --name resource-graph --only-show-errors || true
50+
az extension add --name costmanagement --only-show-errors || true
51+
az extension add --name billing-benefits --only-show-errors || true
52+
az extension add --name quotas --only-show-errors || true
53+
az extension add --name ssh --only-show-errors || true
4154

4255
# Install Google Cloud CLI
4356
curl -sSL https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-cli-516.0.0-linux-x86_64.tar.gz -o /tmp/gcloud.tar.gz

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,4 @@ COPY docker-entrypoint.sh .
7777
RUN chmod +x docker-entrypoint.sh
7878
ENTRYPOINT ["./docker-entrypoint.sh"]
7979

80-
EXPOSE 80
80+
EXPOSE 80

engine.Dockerfile

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,23 @@ RUN <<-```
4040

4141
ENV LD_LIBRARY_PATH=""
4242
ENV AZURE_CONFIG_DIR="/tmp/azure"
43+
ENV AZURE_EXTENSION_DIR="/opt/azure/config/cliextensions"
4344

44-
RUN pip3 install azure-cli==2.74.0 && \
45+
RUN <<-```
46+
set -ex
47+
pip3 install azure-cli==2.74.0
4548
mkdir -p /tmp/azure
49+
mkdir -p /opt/azure/config/cliextensions
50+
if command -v az >/dev/null 2>&1; then
51+
az config set extension.use_dynamic_install=yes_without_prompt
52+
az extension add --name reservation --only-show-errors || true
53+
az extension add --name resource-graph --only-show-errors || true
54+
az extension add --name costmanagement --only-show-errors || true
55+
az extension add --name billing-benefits --only-show-errors || true
56+
az extension add --name quotas --only-show-errors || true
57+
az extension add --name ssh --only-show-errors || true
58+
fi
59+
```
4660

4761
ENV CLOUDSDK_CONFIG="/tmp/gcloud"
4862
RUN <<-```
@@ -97,4 +111,4 @@ RUN ./tools/link-packages.sh
97111
ENV NODE_OPTIONS=--no-node-snapshot
98112

99113
ENTRYPOINT [ "/bin/bash", "-c" ]
100-
CMD [ "cp -r /var/tmp-base/. /tmp/ && /lambda-entrypoint.sh main.handler; exit $?" ]
114+
CMD [ "cp -r /var/tmp-base/. /tmp/ && /lambda-entrypoint.sh main.handler; exit $?" ]

packages/blocks/azure/src/lib/azure-cli.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ import { mkdtempSync } from 'fs';
33
import { tmpdir } from 'os';
44
import { join } from 'path';
55

6+
function setEnvIfExists(envVars: Record<string, string>, key: string): void {
7+
const value = process.env[key];
8+
if (value) {
9+
envVars[key] = value;
10+
}
11+
}
12+
613
export async function runCommand(
714
command: string,
815
credentials: any,
@@ -13,10 +20,8 @@ export async function runCommand(
1320
PATH: process.env['PATH'] || '',
1421
};
1522

16-
const processAzureConfigDir = process.env['AZURE_CONFIG_DIR'];
17-
if (processAzureConfigDir) {
18-
envVars['AZURE_CONFIG_DIR'] = processAzureConfigDir;
19-
}
23+
setEnvIfExists(envVars, 'AZURE_CONFIG_DIR');
24+
setEnvIfExists(envVars, 'AZURE_EXTENSION_DIR');
2025

2126
if (!shouldUseHostCredentials) {
2227
const azureConfigDir = mkdtempSync(join(tmpdir(), 'azure-cli'));

packages/blocks/azure/test/azure-cli.test.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ describe('azureCli', () => {
3131
{
3232
PATH: process.env['PATH'],
3333
AZURE_CONFIG_DIR: expect.any(String),
34+
AZURE_EXTENSION_DIR: expect.any(String),
3435
},
3536
);
3637
expect(commonMock.runCliCommand).toHaveBeenCalledWith(
@@ -39,6 +40,7 @@ describe('azureCli', () => {
3940
{
4041
PATH: process.env['PATH'],
4142
AZURE_CONFIG_DIR: expect.any(String),
43+
AZURE_EXTENSION_DIR: expect.any(String),
4244
},
4345
);
4446
});
@@ -64,6 +66,7 @@ describe('azureCli', () => {
6466
{
6567
PATH: process.env['PATH'],
6668
AZURE_CONFIG_DIR: expect.any(String),
69+
AZURE_EXTENSION_DIR: expect.any(String),
6770
},
6871
);
6972
expect(commonMock.runCliCommand).toHaveBeenNthCalledWith(
@@ -73,6 +76,7 @@ describe('azureCli', () => {
7376
{
7477
PATH: process.env['PATH'],
7578
AZURE_CONFIG_DIR: expect.any(String),
79+
AZURE_EXTENSION_DIR: expect.any(String),
7680
},
7781
);
7882
expect(commonMock.runCliCommand).toHaveBeenNthCalledWith(
@@ -82,6 +86,7 @@ describe('azureCli', () => {
8286
{
8387
PATH: process.env['PATH'],
8488
AZURE_CONFIG_DIR: expect.any(String),
89+
AZURE_EXTENSION_DIR: expect.any(String),
8590
},
8691
);
8792
});
@@ -105,6 +110,7 @@ describe('azureCli', () => {
105110
'az',
106111
{
107112
PATH: process.env['PATH'],
113+
AZURE_EXTENSION_DIR: expect.any(String),
108114
},
109115
);
110116
expect(commonMock.runCliCommand).toHaveBeenNthCalledWith(
@@ -113,6 +119,7 @@ describe('azureCli', () => {
113119
'az',
114120
{
115121
PATH: process.env['PATH'],
122+
AZURE_EXTENSION_DIR: expect.any(String),
116123
},
117124
);
118125
});
@@ -132,6 +139,7 @@ describe('azureCli', () => {
132139
'az',
133140
{
134141
PATH: process.env['PATH'],
142+
AZURE_EXTENSION_DIR: expect.any(String),
135143
},
136144
);
137145
process.env = originalEnv;
@@ -143,6 +151,7 @@ describe('azureCli', () => {
143151
...originalEnv,
144152
PATH: '/mock/path',
145153
AZURE_CONFIG_DIR: '/mock/config/dir',
154+
AZURE_EXTENSION_DIR: '/mock/config/dir/extensions',
146155
};
147156

148157
commonMock.runCliCommand.mockResolvedValue('mock result');
@@ -157,6 +166,44 @@ describe('azureCli', () => {
157166
{
158167
PATH: process.env['PATH'],
159168
AZURE_CONFIG_DIR: process.env['AZURE_CONFIG_DIR'],
169+
AZURE_EXTENSION_DIR: '/mock/config/dir/extensions',
170+
},
171+
);
172+
process.env = originalEnv;
173+
});
174+
175+
test('should use temp config dir but preserve extension dir when useHostSession is false and AZURE_CONFIG_DIR is set', async () => {
176+
const originalEnv = process.env;
177+
process.env = {
178+
...originalEnv,
179+
PATH: '/mock/path',
180+
AZURE_CONFIG_DIR: '/mock/config/dir',
181+
AZURE_EXTENSION_DIR: '/mock/config/dir/extensions',
182+
};
183+
184+
commonMock.runCliCommand.mockResolvedValueOnce('login result');
185+
commonMock.runCliCommand.mockResolvedValueOnce('mock result');
186+
187+
const result = await runCommand('some command', credentials, false);
188+
189+
expect(result).toBe('mock result');
190+
expect(commonMock.runCliCommand).toHaveBeenCalledTimes(2);
191+
expect(commonMock.runCliCommand).toHaveBeenCalledWith(
192+
`login --service-principal --username ${credentials.clientId} --password ${credentials.clientSecret} --tenant ${credentials.tenantId}`,
193+
'az',
194+
{
195+
PATH: process.env['PATH'],
196+
AZURE_CONFIG_DIR: expect.any(String),
197+
AZURE_EXTENSION_DIR: '/mock/config/dir/extensions',
198+
},
199+
);
200+
expect(commonMock.runCliCommand).toHaveBeenCalledWith(
201+
'some command',
202+
'az',
203+
{
204+
PATH: process.env['PATH'],
205+
AZURE_CONFIG_DIR: expect.any(String),
206+
AZURE_EXTENSION_DIR: '/mock/config/dir/extensions',
160207
},
161208
);
162209
process.env = originalEnv;
@@ -176,6 +223,7 @@ describe('azureCli', () => {
176223
{
177224
PATH: process.env['PATH'],
178225
AZURE_CONFIG_DIR: expect.any(String),
226+
AZURE_EXTENSION_DIR: expect.any(String),
179227
},
180228
);
181229
});
@@ -198,6 +246,7 @@ describe('azureCli', () => {
198246
{
199247
PATH: process.env['PATH'],
200248
AZURE_CONFIG_DIR: expect.any(String),
249+
AZURE_EXTENSION_DIR: expect.any(String),
201250
},
202251
);
203252
});

0 commit comments

Comments
 (0)